我在两种情况下使用 UIImagePickerController
- 选择照片库中的现有图像
- 拍一张新照片
在第一种情况下,当我从库中选择图像时,我可以轻松地在委托方法中获取 URL:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Get the URL
NSURL *url = [info valueForKey:UIImagePickerControllerReferenceURL];
...
}
但是当我拍摄一张新照片时,该图像尚未在照片库中,并且还没有 URL。所以,我首先需要在库中添加图像。但是,如何获取新资产的 URL?
这是我在照片库中添加图像的代码
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Get the image
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Add the image in the library
[[PHPhotoLibrary sharedPhotoLibrary]
performChanges:^
{
// Request creating an asset from the image.
PHAssetChangeRequest *createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
// Get the URL of the new asset here ?
...
}
completionHandler:^(BOOL success, NSError *error)
{
if (!success) { ...; return; }
// Get the URL of the new asset here ?
...
}
];
}