Ben*_*Ben 50 iphone save photolibrary uiimage ios
我使用以下方法将合并后的图像保存到iPhone照片库:
UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
Run Code Online (Sandbox Code Playgroud)
并使用以下方式获取回调:
- (void) savedPhotoImage:(UIImage*)image didFinishSavingWithError:(NSError *)error contextInfo: (void *)contextInfo { NSLog(@"%@", [error localizedDescription]);
NSLog(@"info: %@", contextInfo);}
Run Code Online (Sandbox Code Playgroud)
我想得到的是图像保存位置的路径,因此我可以将其添加到一个数组中,该数组将用于调用应用程序中其他位置的已保存项目列表.
当我使用选择器加载图像时,它显示路径信息.但是,当我保存创建的图像时,我无法找到保存图像路径的位置.
我有关于网络的搜索,但是大多数示例都在回调时停止,并提供一条很好的消息,说明图像已成功保存.我只想知道它的保存位置.
我理解一种方法可能是开始定义我自己的路径,但正如该方法为我做的那样,我只是希望它可以告诉我它保存到哪里.
Ben*_*Ben 86
我终于找到了答案.显然UIImage方法剥离了元数据,因此使用UIImageWriteToSavedPhotosAlbum并不好.
然而,在ios4中,Apple推出了一个新的框架来处理名为ALAssetsLibrary的照片库.
首先,您需要右键单击Targets,然后在构建部分中,使用左下方的小+图标将AlAsset Framework添加到项目中.
然后添加#import "AssetsLibrary/AssetsLibrary.h";到您的类的头文件中.
最后,您可以使用以下代码:
UIImage *viewImage = YOUR UIIMAGE // --- mine was made from drawing context
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Request to save the image to camera roll
[library writeImageToSavedPhotosAlbum:[viewImage CGImage] orientation:(ALAssetOrientation)[viewImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
NSLog(@"error");
} else {
NSLog(@"url %@", assetURL);
}
}];
[library release];
Run Code Online (Sandbox Code Playgroud)
这将获得您刚刚保存的文件的路径.