I am using PHImageManagers's requestImageDataForAsset to calculate memory size of particular photos in camera roll. It is working fine in my device but some users getting countinous crash after calculate memory size (found by analytics and crash report).
code and crash report
Please help.
On which iOS version it is crashing, seems working fine for me in iOS 8.3 , if you are exactly using the same process to fetch image meta data or memory size:-
--------------------------------------------------------------
-- get the UIImage instance from a PHAsset
--------------------------------------------------------------
- (UIImage*)grabImageFromAsset:(PHAsset *)asset
{
__block UIImage *returnImage;
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.synchronous = YES;
[[PHImageManager defaultManager] requestImageForAsset:asset
targetSize:CGSizeMake(200, 200)
contentMode:PHImageContentModeAspectFill
options:options
resultHandler:
^(UIImage *result, NSDictionary *info) {
returnImage = result;
}];
return returnImage;
}
--------------------------------------------------------------
-- get the metadata of the image from a PHAsset
--------------------------------------------------------------
- (NSDictionary *)grabImageDataFromAsset:(PHAsset *)asset
{
__block NSMutableDictionary *imageAssetInfo = [NSMutableDictionary new];
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.synchronous = YES;
[[PHImageManager defaultManager] requestImageDataForAsset:asset
options:options
resultHandler:
^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
if ([info[@"PHImageResultIsInCloudKey"] isEqual:@YES]) {
// in the cloud
NSLog(@"in the cloud (sync grabImageDataFromAsset)");
}
imageAssetInfo = [info mutableCopy];
if (imageData) {
imageAssetInfo[@"IMAGE_NSDATA"] = imageData;
}
}];
return imageAssetInfo;
}
I am following this links for my photo kit operation:- phimagemanager and ios-8-photokit
I had an issue similar to this when users had their photos stored in iCloud using iCloud photo library. I was able to fix the issue by making the call asynchronous. It looks like this
let options = PHImageRequestOptions()
options.synchronous = false
options.networkAccessAllowed = true
Here is the whole function.
func getImageDataFromAsset(asset: PHAsset, completion: (data: NSData?) -> Void) {
let manager = PHImageManager.defaultManager()
let options = PHImageRequestOptions()
options.networkAccessAllowed = true
options.synchronous = false
manager.requestImageDataForAsset(asset, options: options) { (result, string, orientation, info) -> Void in
if let imageData = result {
completion(data: imageData)
} else {
completion(data: nil)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With