Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHImageManager's method "requestImageDataForAsset" gives crash in Swift

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.

like image 639
Madhubalan K Avatar asked May 07 '15 14:05

Madhubalan K


2 Answers

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

like image 130
Vizllx Avatar answered Nov 02 '22 10:11

Vizllx


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)
        }
    }
}
like image 21
Micah Wilson Avatar answered Nov 02 '22 08:11

Micah Wilson