【发布时间】:2019-05-28 16:35:59
【问题描述】:
我正在 iOS 中开发一个框架。我的框架中有一个函数,它使用 Photos 框架从相机胶卷中获取最新的图像。此功能在大多数使用我的框架的项目中都能成功运行。但它只在这些项目中的一个项目上崩溃。我尝试了多种方法来修复此崩溃,但在此崩溃中没有任何改变。
我检查了这个项目设置,但我找不到与我的项目设置有任何区别。我还检查了 plist 文件中的画廊权限。还有一个有趣的点。该项目成功使用了我的库的几乎所有功能,但是当我的库尝试访问该项目中的 UIImagePickerController 时,它也会崩溃。
@interface Controller ()
{
UIBarButtonItem *barButtonItem;
}
@property(nonatomic, strong) PHFetchResult *fetchResult;
@property(nonatomic, strong) PHAsset *lastAsset;
@end
// calling this in the main thread
- (void)checkGalleryPermission
{
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusAuthorized)
{
[self setLastGalleryImageToPhotoGallery];
}
else if (status == PHAuthorizationStatusNotDetermined)
{
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status)
{
if (status == PHAuthorizationStatusAuthorized)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self setLastGalleryImageToPhotoGallery];
});
}
}];
}
}
- (void)setLastGalleryImageToPhotoGallery
{
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
self.fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
if (self.fetchResult != nil)
{
self.lastAsset = [self.fetchResult lastObject];
if (self.lastAsset != nil)
{
PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc] init];
imageRequestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;
imageRequestOptions.synchronous = YES; // tried NO
imageRequestOptions.version = PHImageRequestOptionsVersionCurrent;
[[PHImageManager defaultManager] requestImageForAsset:self.lastAsset targetSize:CGSizeMake(42.0f, 42.0f) contentMode:PHImageContentModeAspectFill options:imageRequestOptions resultHandler:^(UIImage *result, NSDictionary *info)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self->barButtonItem setImage:[result imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
});
}];
}
}
}
我将日志放在上述函数中以查找崩溃的位置。它在下面的代码之后崩溃。我无法弄清楚问题所在。项目设置是否有任何限制到达相机胶卷或者这个问题与线程有关?
self.fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
【问题讨论】:
标签: ios objective-c phasset photosframework