[英]How to get the most recent photo from the Camera Roll on iOS by using Photos framework?
我正在iOS中开发一个框架。 我在我的框架中有一个函数,它通过使用Photos框架从Camera Roll获取最新的图像。 此函数在大多数使用我的框架的项目中成功运行。 但它只是在这些项目的一个项目中崩溃。 我尝试了几种方法来修复此崩溃,但在此崩溃中没有任何改变。
我检查了这个项目设置,但是我发现我的项目设置没有任何区别。 我还检查了plist文件中的gallery权限。 还有一个有趣的观点。 这个项目成功地使用了我的库的几乎所有函数,但是当我的库试图在这个项目中到达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];
如果使用UIImagePickerController同样发生崩溃,则很可能是这个设备上的iOS Photo Library存在问题。 iOS Photo Library是一个核心数据库,可能已损坏。 你看过崩溃报告了吗? 如果您看到任何SQL- / Core-Data问题,这将指向数据库问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.