我正在尝试将UIImage保存到相册。我已经尝试过最后一种方法:
-(IBAction)captureLocalImage:(id)sender{
[photoCaptureButton setEnabled:NO];
// Save to assets library
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum: imageView.image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error2)
{
// report_memory(@"After writing to library");
if (error2) {
NSLog(@"ERROR: the image failed to be written");
}
else {
NSLog(@"PHOTO SAVED - assetURL: %@", assetURL);
}
runOnMainQueueWithoutDeadlocking(^{
// report_memory(@"Operation completed");
[photoCaptureButton setEnabled:YES];
});
}];
}
imageView是一个UIImageView,它包含我要保存的图像。 在日志中我得到了“PHOTO SAVED - assetURL:(null)”,照片没有保存到图书馆。
我做错了什么?
答案 0 :(得分:9)
只需使用此波纹管线即可保存照片库中的图像
UIImageWriteToSavedPhotosAlbum(imageView.image,nil,nil,nil);
:)
答案 1 :(得分:2)
查看此sample ..其中介绍了如何使用资源库保存照片..
你甚至可以使用..
UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);
答案 2 :(得分:1)
我使用GPUImage,显然库本身存在某种问题。
上面的代码绝对有用。如果您想了解有关此问题的更多信息:
GPUImagePicture with imageFromCurrentlyProcessedOutput doesn't get UIImage
答案 3 :(得分:1)
对于iOS 11+需要隐私 - 照片库添加使用说明 info.plist
在info.plist中添加 NSPhotoLibraryAddUsageDescription
代码:
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
答案 4 :(得分:0)
由于iOS 8.1 UIKit框架具有UishmageWriteToSavedPhotosAlbum()函数,因为vishy说。
我以这种方式使用它:
UIImageWriteToSavedPhotosAlbum(myImage, self, "image:didFinishSavingWithError:contextInfo:", nil)
然后
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
// check error, report image has been saved, ...
}
答案 5 :(得分:0)
从相机拍摄照片时,您可以将图像保存在选择器代理中:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];
[library writeImageToSavedPhotosAlbum:image.CGImage metadata:[info objectForKey:UIImagePickerControllerMediaMetadata] completionBlock:^(NSURL *assetURL, NSError *error) {
...
}];
[picker dismissViewControllerAnimated:YES completion:nil];
}