0

I am trying to go through and clean up some deprecation warnings by updating ALAsset with PHPhoto. I am getting confused by this code and need some help to make sure I am not missing something. I added in some comments about what is confusing me.

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

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSURL *assetsURL = [info objectForKey:UIImagePickerControllerReferenceURL];
UIImage *pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];

NSMutableDictionary *metadata = [NSMutableDictionary dictionary];

// This is where I am getting confused.
// What is the difference between the meta data of the else statement
// and the if statement. 
if (assetsURL) {
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(queue, ^{
        [library assetForURL:assetsURL
            resultBlock:^(ALAsset *asset) {
                [metadata addEntriesFromDictionary:asset.defaultRepresentation.metadata];
                dispatch_semaphore_signal(sema);
            }
            failureBlock:^(NSError *error) {
                dispatch_semaphore_signal(sema);
            }];
    });
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
} else {
    [metadata addEntriesFromDictionary:[info objectForKey:UIImagePickerControllerMediaMetadata]];
}
// Undocumented method guessing it rotates image to Portrait
pickedImage = [pickedImage IN_rotateToPortraitOrientation];
[metadata setImageOrientation:UIImageOrientationUp];

if (!assetsURL) {
    [library writeImageToSavedPhotosAlbum:pickedImage.CGImage
                                 metadata:metadata
                          completionBlock:^(NSURL *assetURL, NSError *error) {
                              $l(@"Saved to photo album");
                          }];
}
...
}

If you could help me understand the difference in meta data. If there is a difference how could I accomplish the same thing with PHPhotos.

Jaybit
  • 1,864
  • 1
  • 13
  • 20
  • metadata is NSMutableDictionary. `addEntriesFromDictionary` and `setImageOrientation` must be category methods for NSDictionary. Check your code. – luckyShubhra Sep 06 '17 at 03:15
  • @luckyShubhra That is not at all what I am asking. I know metadata is a Dictionary the problem is what is the difference between UIImagePickerControllerMediaMetadata and asset.defaultRepresentation.metadata. – Jaybit Sep 06 '17 at 03:21
  • `UIImagePickerControllerMediaType` returns NSString object containing a type code such as kUTTypeImage or kUTTypeMovie. whereas `asset.defaultRepresentation.metadata` returns a dictionary of dictionaries of metadata for the representation. – luckyShubhra Sep 06 '17 at 03:42
  • if the user edits a picked item(such as by cropping an image) the URL continues to point to the original version of the picked item. Hence it is checked by accessing assets url `NSURL *assetsURL = [info objectForKey:UIImagePickerControllerReferenceURL];` . If assetsURL is valid i.e. user is editing a picked item metadata for that image already existed and its metadata is accessed by asset.defaultRepresentation.metadata if not it extracts information from UIImagePickerControllerMediaMetadata and save to dictionary. This metadata is saved with image to library. – luckyShubhra Sep 06 '17 at 03:48
  • I have tried and explained do let me know if you have any further queries. – luckyShubhra Sep 06 '17 at 03:48
  • 'UIImagePickerControllerMediaMetadata' returns Metadata for a newly-captured photograph.( image picker whose source type is set to camera) whereas asset.defaultRepresentation.metadata returns a dictionary of dictionaries of metadata for the representation for existing assest url. – luckyShubhra Sep 06 '17 at 04:05

0 Answers0