PhotoKit - didFinishPickingMediaWithInfo vs creationRequestForAssetFromImage(数据不等于??)

时间:2016-08-22 02:05:53

标签: ios objective-c uiimagepickercontroller phasset photokit

想要得到一些反馈,看看我在这里可能会缺少什么。基本上我使用UIImagePickerViewController来拍照。当我完成后,我会像这样检索这个图像:

UIImage *photoTaken = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

我拍完照片后,我需要能够加载相机胶卷中的所有图像并高亮显示我刚拍摄的照片(我显示相机胶卷中的所有图像)。由于这些照片是内存中的不同对象(不同的视图控制器),因此比较它们的唯一方法是比较代表图像的实际数据。 i..e ..

NSData *alreadySelectedPhotoData = UIImageJPEGRepresentation(alreadySelectedPhoto.photoImage, 0.0);
NSData *cameralRollPhotoData = UIImageJPEGRepresentation(cameraRollPhoto.photoImage, 0.0);

 if([cameralRollPhotoData isEqualToData:alreadySelectedPhotoData]){
      //do something here if they are equal(draw a border, etc)
  }

然而,尽管所显示的图像完全相同,但基于这种比较,照片实际上并不相同。

所以我回到原始代码,进行了一些挖掘并进行了数据和视觉测试:

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

       __block UIImage *photoTaken = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
       __block PHObjectPlaceholder *placeholderAsset = nil;

       //save our new photo to the camera roll album(successfully)
       [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
       PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:photoTaken];
       changeRequest.creationDate = creationTimeStamp = [NSDate date];
        placeholderAsset = changeRequest.placeholderForCreatedAsset;
            }
        completionHandler:^(BOOL success, NSError *error){
        PHImageManager *manager = [PHImageManager defaultManager];
        PHImageRequestOptions *requestOptions = [PHImageRequestOptions new];
        requestOptions.resizeMode   = PHImageRequestOptionsResizeModeExact;
        requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
        CGFloat dimension = [UIScreen mainScreen].bounds.size.width / 3 * [UIScreen mainScreen].scale;
        CGSize targetSize = CGSizeMake(dimension, dimension);

        PHFetchResult *savedAssets = [PHAsset fetchAssetsWithLocalIdentifiers:@[placeholderAsset.localIdentifier] options:nil];

        [manager requestImageForAsset:savedAssets.firstObject targetSize:targetSize contentMode:PHImageContentModeAspectFill options:requestOptions resultHandler:^(UIImage *result, NSDictionary *info) {
        //images are the 'same' but their NSData representations appear to not be. NSLog statement never executes.
                NSData *alreadySelectedPhotoData = UIImageJPEGRepresentation(photoTaken, 0.0);
                NSData *cameralRollPhotoData = UIImageJPEGRepresentation(result, 0.0);
               if([cameralRollPhotoData isEqualToData:alreadySelectedPhotoData]){

                     NSLog(@"images are equal");
                    }
                }];

        }];

总结一下:

  1. 将信息对象中返回的图像存储在选择器委托方法中

  2. 使用此图像并使用' creationRequestForAssetFromImage'

  3. 将其存储在相机胶卷中
  4. 通过获取资产(' fetchAssetsWithLocalIdentifiers')

  5. 来检索我们刚刚存储的图像
  6. 将该资产转换回图像(PHManager - requestImageForAsset)

  7. 将通过选取器委托返回的原始UIImage和从创建的相机胶卷返回的图像转换为NSData对象。

  8. 结果:即使屏幕上的图像完全相同,它们也不相等。

    结论:在我看来,如下所示:

    PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:photoTaken];
    

    成功保存到相机胶卷,可以验证它显示的图像是否是我从UIPickerImage委托方法获得的图像(视觉上看起来相同),但是当将两个图像转换为NSData对象时,比较失败。

    有没有人知道这里发生了什么?我错过了什么或这是一个错误吗?

0 个答案:

没有答案