我正在用相机捕捉图像。捕获后,我想获取网址,以便我可以获取图像名称。我正在使用下面的代码。
PHObjectPlaceholder *assetholder;
PHPhotoLibrary *objPhoto = [PHPhotoLibrary sharedPhotoLibrary];
[objPhoto performChanges:^{
PHAssetChangeRequest *assets = [PHAssetChangeRequest creationRequestForAssetFromImage:info[UIImagePickerControllerOriginalImage]];
PHObjectPlaceholder *assetholder = assets.placeholderForCreatedAsset;
} completionHandler:^(BOOL success, NSError * _Nullable error) {
}];
建议我如何获取图像的网址,以便我可以获取图像名称。
答案 0 :(得分: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"];
}
}];
}