将图像保存到照片库并获取保存图像的 URL
Posted
技术标签:
【中文标题】将图像保存到照片库并获取保存图像的 URL【英文标题】:Saving image to Photo Library and get the URL of the saved image 【发布时间】:2014-01-29 20:23:44 【问题描述】:我正在尝试将图像从相机保存到照片库,然后存储已保存图像的 URL。我的代码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
[library writeImageToSavedPhotosAlbum:(__bridge CGImageRef)([info objectForKey:UIImagePickerControllerOriginalImage])
orientation:ALAssetOrientationUp completionBlock:^(NSURL *assetURL, NSError *error)
if(error == nil)
_myImageUrl = [NSString stringWithFormat:@"%@",assetURL];
NSLog(@"%@",assetURL);
else NSLog(@"%@",error);
];
[picker dismissViewControllerAnimated:YES completion:nil];
我的问题是assetUrl 总是NULL。有任何想法吗? 谢谢!
【问题讨论】:
可能与这篇文章有关。可能是 ios7 的错误。 [ALAssetsLibraryWriteVideoCompletionBlock 返回未定义的值][1] [1]:***.com/questions/20813540/… 【参考方案1】:终于,在阅读了更多之后,我找到了答案:函数的第一个参数是CGImageRef
类型,而(__bridge CGImageRef)
没有达到我的预期;但是如果我调用UIImage
对象的CGImage
方法,它就可以工作。正确的做法是:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
CGImageRef image = [[info objectForKey:UIImagePickerControllerOriginalImage] CGImage];
[library writeImageToSavedPhotosAlbum:image
orientation:ALAssetOrientationUp
completionBlock:^(NSURL *assetURL, NSError *error)
if(error == nil)
_myImageUrl = [NSString stringWithFormat:@"%@",assetURL];
NSLog(@"%@",assetURL);
else NSLog(@"%@",error);
];
self.toDoImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
【讨论】:
以上是关于将图像保存到照片库并获取保存图像的 URL的主要内容,如果未能解决你的问题,请参考以下文章