了解旧的ALAsset代码

时间:2017-09-06 03:01:52

标签: ios objective-c alasset phphotolibrary

我正在尝试通过使用PHPhoto更新ALAsset来清理一些弃用警告。我对此代码感到困惑,需要一些帮助,以确保我没有遗漏一些东西。我在一些评论中添加了令我困惑的内容。

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

如果你能帮助我理解元数据的差异。如果有差异我怎么能用PHPhotos完成同样的事情。

0 个答案:

没有答案