- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在最新版本的 iOS(iOS 9.2 和 iOS 9.3)上,获取 PHAssets 现在崩溃了。它以前工作正常。
我得到的错误是:
[PHCollectionList canContainCustomKeyAssets]: unrecognized selector sent to instance Terminating app due to uncaught exception 'NSInvalidArgumentException'
抛出异常的行是:
PHFetchResult *fetchImage = [PHAsset fetchKeyAssetsInAssetCollection:(PHAssetCollection*)collection options:fetchOptions];
更多代码,供引用:
Class PHPhotoLibrary_class = NSClassFromString(@"PHPhotoLibrary");
if (PHPhotoLibrary_class) {
PHFetchResult *fetchResult = self.collectionsFetchResults[indexPath.section];
PHCollection *collection = fetchResult[indexPath.row];
cell.textLabel.text = collection.localizedTitle;
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *fetchImage = [PHAsset fetchKeyAssetsInAssetCollection:(PHAssetCollection*)collection options:fetchOptions];
PHAsset *asset = [fetchImage firstObject];
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.resizeMode = PHImageRequestOptionsResizeModeExact;
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat dimension = 90.0;
CGSize size = CGSizeMake(dimension*scale, dimension*scale);
[[PHImageManager defaultManager] requestImageForAsset:asset
targetSize:size
contentMode:PHImageContentModeAspectFill
options:options
resultHandler:^(UIImage *result, NSDictionary *info) {
if (result) {
CGSize itemSize = CGSizeMake(60, 60);
UIGraphicsBeginImageContextWithOptions(itemSize, NO, 2);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[result drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
else{
UIImage *placeholder = [UIImage imageNamed:@"image-placeholder.jpg"];
CGSize itemSize = CGSizeMake(60, 60);
UIGraphicsBeginImageContextWithOptions(itemSize, NO, 2);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[placeholder drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}];
}
最佳答案
这个问题其实出在这里:
PHCollection *collection = fetchResult[indexPath.row];
然后在这里:
PHAsset *asset = [fetchImage firstObject];
您正在使用 PHCollection
获取集合,然后假设所有 Assets 都是 PHAssets
而没有正确检查是否是这种情况。
实际上,PHCollection 有 two possible subclasses : PHAsset
和 PHCollectionList
,而 PHCollectionList
是这里引发错误的原因。
在 PHCollection *collection = fetchResult[indexPath.row];
之后包装代码并检查 PHAsset
,它应该可以解决问题:
if ([collection isKindOfClass:[PHAssetCollection class]]) {
//code
}
关于ios - [PHCollectionList canContainCustomKeyAssets] : unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36619351/
在最新版本的 iOS(iOS 9.2 和 iOS 9.3)上,获取 PHAssets 现在崩溃了。它以前工作正常。 我得到的错误是: [PHCollectionList canContainCusto
我是一名优秀的程序员,十分优秀!