Onur Şahindur
Onur Şahindur

Reputation: 500

PHImageManager getting photo incorrectly because of edit from photo library

I have a strange issue about getting UIImages with PHImageManager.

Everything works fine with when the requested UIImage is not edited from photos app of iPhone;

[[PHImageManager defaultManager] requestImageForAsset:[assets objectAtIndex:0] targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:requestOptions resultHandler:^void(UIImage *image, NSDictionary *info)
         {
             DebugLog(@"%ld", assets.count);
             @autoreleasepool
             {
                 if (image)
                 {
                     // Write image to system
                     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                     NSString *fileName = [NSString stringWithFormat:@"%ld_%@.jpg", (long)assets.count, [NSDate date]];
                     NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileName];
                     [UIImageJPEGRepresentation(image, 0.5) writeToFile:filePath atomically:YES];

                 }
         }];

But in case that if a user edited the photo from iPhone's self photo editor (i.e. s/he cropped the picture) the PHImageManager fails to retrieve that photo.

Without editing, the dictionary provided in the resultHandler is normal which is called "info":

{
PHImageFileOrientationKey = 0;
PHImageFileSandboxExtensionTokenKey = "5565e83d376d8c4e018b8ea41401e58e5aabbc18;00000000;00000000;000000000000001a;com.apple.app-sandbox.read;00000001;01000003;000000000039a762;/private/var/mobile/Media/DCIM/113APPLE/IMG_3433.PNG";
PHImageFileURLKey = "file:///var/mobile/Media/DCIM/113APPLE/IMG_3433.PNG";
PHImageFileUTIKey = "public.png";
PHImageResultDeliveredImageFormatKey = 9999;
PHImageResultIsDegradedKey = 0;
PHImageResultIsInCloudKey = 0;
PHImageResultIsPlaceholderKey = 0;
PHImageResultOptimizedForSharing = 0;
PHImageResultRequestIDKey = 55;
PHImageResultWantedImageFormatKey = 9999;

}

But after editing from photo application the dictionary becomes:

{
PHImageFileOrientationKey = 0;
PHImageResultDeliveredImageFormatKey = 4031;
PHImageResultIsDegradedKey = 1;
PHImageResultRequestIDKey = 56;
PHImageResultWantedImageFormatKey = 9998;

}

Does anyone faced with this issue before?

Thank you for your replies.

Upvotes: 0

Views: 1690

Answers (2)

JPetric
JPetric

Reputation: 3918

I am too late to help you now, but maybe someone else will have the same issue in the future.

Instead of PHImageRequestOptionsVersionUnadjusted use PHImage​Request​Options​Delivery​Mode​High​Quality​Format.

Or in Swift use,

requestOptions.deliveryMode = PHImageRequestOptionsDeliveryMode.highQualityFormat

If you check the documentation for highQualityFormat:

Photos provides only the highest-quality image available, regardless of how much time it takes to load. If the isSynchronous property is true or if using the requestImageData(for:options:resultHandler:) method, this behavior is the default and only option (that is, specifying other delivery mode options has no effect).

Upvotes: 0

Onur Şahindur
Onur Şahindur

Reputation: 500

Okey I find out what happens there.

In PHImageRequestOptions there is an option to choose the unadjusted version of the photo by adding;

requestOptions.version = PHImageRequestOptionsVersionUnadjusted;

With this, I successfully get the Image (however in unadjusted format).

Anyone could find further solution without taking the image adjusted, please comment me.

Cheers.

Upvotes: 1

Related Questions