1

Wanted to get some feedback to see what I might be missing here. Basically I use a UIImagePickerViewController to take a photo. When I am done i retrieve this image like this:

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

After I have taken the photo, at a later time, I need to be able load all the images in my camera roll and highlight the photo(I display all the images from the camera roll) that I just took. Because these photos are different objects in memory (different view controllers), the only way to compare them is by comparing the actual data that represents the images. 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)
  }

However, the photos never actually were equal based on this comparison, despite the fact that the images displayed are identical.

So I went back to the original code, did some digging and did a data and visual test:

 - (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");
                    }
                }];

        }];

So to summarize:

  1. store the image that comes back from the info object in the picker delegate method

  2. use this image and store it in the camera roll by using 'creationRequestForAssetFromImage'

  3. retrieve back the image that we just stored by getting Asset ('fetchAssetsWithLocalIdentifiers')

  4. convert that asset back into an image (PHManager - requestImageForAsset)

  5. convert both the original UIImage that was returned via the picker delegate and the image back from the camera roll that was created to NSData objects.

Result: they do not equal, even though the images on the screen are exactly the same.

Conclusion: It seems to me that this below:

PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:photoTaken];

saves to the camera roll successfully, can verify that the image it displays is exactly the image that I got from the UIPickerImage delegate method(visually looks the same), yet when converting both images to NSData objects the comparison fails.

Does anyone have any idea whats going on here? did I miss something or is this a bug?

cspam
  • 2,911
  • 2
  • 23
  • 41

0 Answers0