0

我正在用相机捕捉图像。捕获后,我想获取 URL,以便获取图像名称。我为此使用以下代码。

 PHObjectPlaceholder *assetholder;
        PHPhotoLibrary *objPhoto = [PHPhotoLibrary sharedPhotoLibrary];
        [objPhoto performChanges:^{

        PHAssetChangeRequest *assets = [PHAssetChangeRequest creationRequestForAssetFromImage:info[UIImagePickerControllerOriginalImage]];
        PHObjectPlaceholder *assetholder = assets.placeholderForCreatedAsset;

        } completionHandler:^(BOOL success, NSError * _Nullable error) {


        }];

建议我如何获取图像的 url,以便我可以获取图像名称。

4

1 回答 1

1
PHAsset *asset = nil;
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
if (fetchResult != nil && fetchResult.count > 0) {
    // get last photo from Photos
    asset = [fetchResult lastObject];
}

if (asset) {
    // get photo info from this asset
    PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init];
    imageRequestOptions.synchronous = YES;
    [[PHImageManager defaultManager]
             requestImageDataForAsset:asset
                            options:imageRequestOptions
                      resultHandler:^(NSData *imageData, NSString *dataUTI,
                                      UIImageOrientation orientation, 
                                      NSDictionary *info) 
     {
          NSLog(@"info = %@", info);
          if ([info objectForKey:@"PHImageFileURLKey"]) {
               // path looks like this - 
               // file:///var/mobile/Media/DCIM/###APPLE/IMG_####.JPG
               NSURL *path = [info objectForKey:@"PHImageFileURLKey"];
     }                                            
    }];
}
于 2017-03-01T07:31:01.430 回答