iPhone:如何获取使用UIImageWriteToSavedPhotosAlbum()保存的图像的文件路径?
我使用保存合并图像到iPhone照片库:iPhone:如何获取使用UIImageWriteToSavedPhotosAlbum()保存的图像的文件路径?
UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
并获得使用回调:
- (void) savedPhotoImage:(UIImage*)image didFinishSavingWithError:(NSError *)error contextInfo: (void *)contextInfo { NSLog(@"%@", [error localizedDescription]);
NSLog(@"info: %@", contextInfo);}
我想得是在图像具有路径已保存,所以我可以将它添加到数组中,该数组将用于调用应用程序中其他位置保存的项目列表。
当我使用选取器加载图像时,它显示路径信息。 但是,当我保存一个创建的图像时,我找不到将保存的图像路径拉到哪里。
我有关于网络的搜索,但大多数例子停止在回调与一个不错的消息说图像保存成功。 我只想知道它的保存位置。
我明白一种方法可能是开始定义我自己的路径,但由于该方法对我来说,我只是希望它能告诉我它保存在哪里。
我终于找出答案。 显然,UIImage方法去掉元数据,所以使用UIImageWriteToSavedPhotosAlbum并不好。
但是,在ios4中,Apple放入了一个新的框架来处理称为ALAssetsLibrary的照片库。
首先,您需要右键单击目标,然后在构建部分中,使用左下角的小图标将AlAsset框架添加到项目中。
然后将#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];
这让你刚刚保存的文件的路径。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
RandomIndexnew = arc4random() % 3;
if(RandomIndexnew == 0)
{
nameStr =[NSString stringWithFormat:@"jpg"];
textFieldNormalFile_type.text =[NSString stringWithFormat:@"jpg"];
}
else if(RandomIndexnew = 1)
{
nameStr =[NSString stringWithFormat:@"gif"];
textFieldNormalFile_type.text =[NSString stringWithFormat:@"GIF"];
}
else if(RandomIndexnew = 2)
{
nameStr =[NSString stringWithFormat:@"jpg"];
textFieldNormalFile_type.text =[NSString stringWithFormat:@"JPG"];
}
RandomIndex = arc4random() % 20;
NSString *nameStr1 =[NSString stringWithFormat:@"Image%i",RandomIndex];
textFieldNormalFile_name.text =[NSString stringWithFormat:@"%@.%@",nameStr1,nameStr];
newFilePath = [NSHomeDirectory() stringByAppendingPathComponent: textFieldNormalFile_name.text];
imageData = UIImageJPEGRepresentation(img, 1.0);
if (imageData != nil) {
NSLog(@"HERE [%@]", newFilePath);
[imageData writeToFile:newFilePath atomically:YES];
}
image.image =[UIImage imageNamed:newFilePath];
NSLog(@"newFilePath:%@",newFilePath);
path.text =[NSString stringWithFormat:newFilePath];
NSLog(@"path.text :%@",path.text);
}
我正在实施这个代码我自己的应用程序和它的工作..你可以修改你自己的代码来实现这一点。我希望它能帮助你。 – sandy 2010-12-16 07:57:21
我的代码是
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
imageURL = nil;
ALAssetsLibraryWriteImageCompletionBlock completeBlock = ^(NSURL *assetURL, NSError *error){
if (!error) {
#pragma mark get image url from camera capture.
imageURL = [NSString stringWithFormat:@"%@",assetURL];
}
};
if(image){
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:[image CGImage]
orientation:(ALAssetOrientation)[image imageOrientation]
completionBlock:completeBlock];
}
}
在.H导入库
,并定义ALAssetsLibraryWriteImageCompletionBlock
#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>
typedef void (^ALAssetsLibraryWriteImageCompletionBlock)(NSURL *assetURL, NSError *error);
的类型定义,如果你不知道如何获得<AssetsLibrary/AssetsLibrary.h>
,请添加现有框架(AssetsLibrary.framework)
Swift Version将为
ALAssetsLibrary().writeImageToSavedPhotosAlbum(editedImage.CGImage, orientation: ALAssetOrientation(rawValue: editedImage.imageOrientation.rawValue)!,
completionBlock:{ (path:NSURL!, error:NSError!) -> Void in
print("\(path)")
})
和“在您的文件中导入ALAssetsLibrary”。
项目 - >构建阶段 - >链接二进制 - > AssetsLibrary.framework
你可以更新它的Swift 2.0吗? – 2016-06-06 18:10:18
ALAssetsLibrary已被弃用。
这是你应该做的方式:
#import <Photos/Photos.h>
UIImage *yourImage;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest creationRequestForAssetFromImage:yourImage];
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
NSLog(@"Success");
} else {
NSLog(@"write error : %@",error);
}
}];
不错!感谢分享。 – 2013-09-01 20:36:21
如何加载该图像?我可以加载图像窗体画廊?任何人都可以发布代码来加载? – 2016-03-02 14:33:53
ALAssetsLibrary在iOS 9.0以上版本中已弃用。而是使用PHPhotoLibrary和“performChanges”方法将图像保存到设备。 – Tys 2016-06-03 14:31:52