I am developing a framework in iOS. I have a function in my framework that it gets the most recent image from the Camera Roll by using Photos framework. This function works successfully in most of projects which use my framework. But it crashes on only one project from these projects. I tried several ways to fix this crash, but nothing changed in this crash.
I checked this project settings but I could not find any difference from my project settings. Also I checked the gallery permission in the plist file. Also there is an interesting point. This project uses almost all functions of my library successfully but it also crashes when my library tries to reach UIImagePickerController in this project.
@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]];
});
}];
}
}
}
I put logs in the above functions to find where it crashes. And it crashes after the below code. I could not figure out the problem. Is there any restriction in the project setting to reach Camera Roll or is this problem related to thread?
self.fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
If the same crash happens also using UIImagePickerController there is most likely an issue with the iOS Photo Library on this one device. The iOS Photo Library is a Core-Data database under the hood and can be corrupt. Did you have a look at the crash report. If you see any SQL-/Core-Data issues this would point to database problems.