gpt4 book ai didi

iphone - ALAssetsLibrary - 收到 ALAssetsLibraryChangedNotification 后崩溃

转载 作者:技术小花猫 更新时间:2023-10-29 11:05:04 25 4
gpt4 key购买 nike

我的应用程序的一部分有一个照片浏览器,有点类似于 Apple 的照片应用程序,带有一个用于浏览照片缩略图的初始 View Controller 和一个在您点击照片时显示的详细 View 。

我正在使用 ALAssetsLibrary 访问照片,并将一组 ALAsset URL 传递到我的详细信息 View Controller ,以便您可以从一张照片滑动到下一张照片。

一切正常,直到我在从一张照片滑动到另一张照片时收到 ALAssetsLibraryChangedNotification(在详细 View Controller 中),这通常会导致崩溃:

NOTIFICATION: the asset library changed // my own NSLog for when the notification occurs

loading assets... // my own NSLog for when I start reloading assets in the thumbnail browser

Assertion failed: (size == bytesRead), function -[ALAssetRepresentation _imageData], file /SourceCache/AssetsLibrary/MobileSlideShow-1373.58.1/Sources/ALAssetRepresentation.m, line 224.

它崩溃的具体代码行是在调用 [currentRep metadata] 中,如下所示:

- (void)someMethod {
NSURL *assetURL = [self.assetURLsArray objectAtIndex:index];
ALAsset *currentAsset;

[self.assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {

[self performSelectorInBackground:@selector(configureDetailViewForAsset:) withObject:asset];

} failureBlock:^(NSError *error) {
NSLog(@"failed to retrieve asset: %@", error);
}];
}

- (void)configureDetailViewForAsset:(ALAsset *)currentAsset {
ALAssetRepresentation *currentRep = [currentAsset defaultRepresentation];

if (currentAsset != nil) {
// do some stuff
}
else {
NSLog(@"ERROR: currentAsset is nil");
}

NSDictionary *metaDictionary;
if (currentRep != nil) {
metaDictionary = [currentRep metadata];

// do some other stuff
}
else {
NSLog(@"ERROR: currentRep is nil");
}
}

我知道一旦收到通知,它就会使对 ALAsset 和 ALAssetRepresentation 对象的任何引用无效...但是我应该如何处理它在尝试访问它的过程中使某些内容无效的情况?

我已经尝试设置一个 BOOL,在收到通知时立即完全中止并阻止 [currentRep metadata] 被调用,但即使这样也不是每次都能捕捉到它:

if (self.receivedLibraryChangeNotification) {
NSLog(@"received library change notification, need to abort");
}
else {
metaDictionary = [currentRep metadata];
}

有什么我可以做的吗?在这一点上,我几乎准备放弃使用 ALAssetsLibrary 框架。

(请注意 Apple 开发论坛上描述相同问题的未解决线程:https://devforums.apple.com/message/604430)

最佳答案

看来问题出在这里:

[self.assetsLibrary assetForURL:nextURL 

resultBlock:^(ALAsset *asset) {
// You should do some stuff with asset at this scope
ALAssetRepresentation *currentRep = [asset defaultRepresentation];
// Assume we have a property for that
self.assetRepresentationMetadata = [currentRep metadata];
...
// assume we have a method for that
[self updateAssetDetailsView];
}

failureBlock:^(NSError *error) {
NSLog(@"failed to retrieve asset: %@", error);
}];

获得用户 Assets 后,最好通过向详细信息 Controller subview 提供必要的数据或通过缓存以备后用来复制 Assets 信息。它有助于避免 ALAsset 失效问题。当发送通知 ALAssetsLibraryChangedNotification 时,您可能需要丢弃详细信息 Controller 并从头开始查询库内容。

关于iphone - ALAssetsLibrary - 收到 ALAssetsLibraryChangedNotification 后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10637368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com