Jim*_*des 10 iphone ios alasset alassetslibrary
我的应用程序的一部分,具有图片浏览器,有点类似于苹果的照片应用程序,最初的视图控制器来浏览照片缩略图,当你在照片上轻按显示真实的细节图.
我使用ALAssetsLibrary访问的照片,和我通过的ALAsset URL的阵列,以我的详细信息视图控制器,这样你就可以从一张照片到下一个刷卡.
一切都很好,直到我从一张照片到另一张照片(在详细视图控制器中)滑动时收到ALAssetsLibraryChangedNotification,这通常会导致崩溃:
通知:资产库在发生通知时更改了我自己的NSLog
在我开始在缩略图浏览器中重新加载资源时加载资产... //我自己的NSLog
断言失败:(size == bytesRead),function - [ALAssetRepresentation _imageData],file /SourceCache/AssetsLibrary/MobileSlideShow-1373.58.1/Sources/ALAssetRepresentation.m,第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");
}
}
Run Code Online (Sandbox Code Playgroud)
我知道一旦收到通知,它就会使对ALAsset和ALAssetRepresentation对象的任何引用无效......但是我应该如何处理它在尝试访问它的过程中无效的情况呢?
我已经试过接到通知时完全中止,防止[currentRep元]从曾经被称为设置BOOL,右,但即使这样,每次不抓住它:
if (self.receivedLibraryChangeNotification) {
NSLog(@"received library change notification, need to abort");
}
else {
metaDictionary = [currentRep metadata];
}
Run Code Online (Sandbox Code Playgroud)
有什么我能做的吗?在这一点上,我几乎已经准备好放弃使用ALAssetsLibrary框架.
(注意Apple dev论坛上这个未解决的帖子描述了同样的问题: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);
}];
Run Code Online (Sandbox Code Playgroud)
获得用户资产后,最好通过向详细控制器子视图提供必要的数据或通过缓存以供以后使用来复制资产信息.避免ALAsset失效问题会有所帮助.当通知ALAssetsLibraryChangedNotification发送时,您可能需要丢弃详细信息控制器并从头开始查询库内容.
| 归档时间: |
|
| 查看次数: |
2595 次 |
| 最近记录: |