PHImageManager 由于从照片库编辑而错误地获取照片

PHImageManager getting photo incorrectly because of edit from photo library

关于使用 PHImageManager 获取 UIImage,我遇到了一个奇怪的问题。

如果请求的 UIImage 不是从 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];

                 }
         }];

但是,如果用户从 iPhone 的自拍照片编辑器编辑照片(即 s/he 裁剪照片),PHImageManager 将无法检索该照片。

没有编辑,resultHandler中提供的字典是正常的,叫做"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;

}

但是从照片应用编辑后字典变成:

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

}

有人遇到过这个问题吗?

感谢您的回复。

好吧,我知道那里发生了什么。

在 PHImageRequestOptions 中,有一个选项可以通过添加来选择照片的未调整版本;

requestOptions.version = PHImageRequestOptionsVersionUnadjusted;

有了这个,我成功地得到了图像(但是是未调整的格式)。

谁能在不调整图片的情况下找到进一步的解决方案,请评论我。

干杯。

我现在来不及帮你了,但也许以后会有其他人遇到同样的问题。

而不是 PHImageRequestOptionsVersionUnadjusted 使用 PHImage​Request​Options​Delivery​Mode​High​Quality​Format.

或在Swift中使用,

requestOptions.deliveryMode = PHImageRequestOptionsDeliveryMode.highQualityFormat

如果您查看 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).