I'm saving a merged image to the iPhone photo library using:
我将合并后的图像保存到iPhone照片库中,使用:
UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
And getting the callback using:
并使用:
- (void) savedPhotoImage:(UIImage*)image didFinishSavingWithError:(NSError *)error contextInfo: (void *)contextInfo { NSLog(@"%@", [error localizedDescription]);
NSLog(@"info: %@", contextInfo);}
What I would like to get is the path for where the image has been saved, so I can add it to an array which will be used to call up a list of saved items elsewhere in the app.
我想要的是图像保存的路径,所以我可以将它添加到一个数组中,这个数组将被用于调用app中其他地方的保存的项目列表。
When I load the image using the picker it displays the path info. However when I save a created image, I can't find where to pull the saved image path.
当我使用picker加载图像时,它将显示路径信息。然而,当我保存创建的图像时,我无法找到在哪里提取保存的图像路径。
I have search about the web, but most examples stop at the callback with a nice message to say the image was saved successfully. I would just like to be able to know where it was saved.
我搜索了有关web的内容,但是大多数示例都在回调处停止,并给出了一个很好的消息,表示图像已成功保存。我只是想知道它被保存在哪里。
I understand one method might be to start defining my own paths, but as the method does that for me, I was just hoping it could tell me where it saved to.
我知道一个方法可能是开始定义我自己的路径,但是当这个方法为我这样做时,我只是希望它能告诉我它保存到哪里。
5 个解决方案
#1
85
I finally found out the answer. Apparently the UIImage methods strip out metadata and so using UIImageWriteToSavedPhotosAlbum is no good.
我终于找到了答案。显然,UIImage方法去掉了元数据,所以使用UIImageWriteToSavedPhotosAlbum不是好方法。
However in ios4 Apple put in a new framework to handle the photo library called the ALAssetsLibrary.
然而,在ios4中,苹果加入了一个新的框架来处理名为ALAssetsLibrary的照片库。
First you need to right click on the Targets and in the build part, add the AlAsset Framework to your project with the little + icon in the bottom left.
首先,您需要右键单击目标,在构建部分,将AlAsset框架添加到项目中,在左下角有一个+图标。
Then add #import "AssetsLibrary/AssetsLibrary.h";
to the header file of your class.
然后添加#进口“AssetsLibrary / AssetsLibrary.h”;到类的头文件。
Finally you can use the following code:
最后,您可以使用以下代码:
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];
And that gets the path of the file you just saved.
得到刚才保存的文件的路径。
#2
3
My code is
我的代码是
-(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];
}
}
in .h import Library and define type def of ALAssetsLibraryWriteImageCompletionBlock
在.h导入库中定义ALAssetsLibraryWriteImageCompletionBlock的类型def
#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>
typedef void (^ALAssetsLibraryWriteImageCompletionBlock)(NSURL *assetURL, NSError *error);
if you don't know how to get <AssetsLibrary/AssetsLibrary.h>
, please add existing framework (AssetsLibrary.framework)
如果您不知道如何获得
#3
1
Swift Version will be
斯威夫特版本将
ALAssetsLibrary().writeImageToSavedPhotosAlbum(editedImage.CGImage, orientation: ALAssetOrientation(rawValue: editedImage.imageOrientation.rawValue)!,
completionBlock:{ (path:NSURL!, error:NSError!) -> Void in
print("\(path)")
})
And "import ALAssetsLibrary" in your file.
和“import ALAssetsLibrary”在您的文件中。
Project-> Build Phases -> Link binary -> AssetsLibrary.framework
项目->构建阶段->链接二进制-> AssetsLibrary.framework
#4
0
- (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);
}
#5
0
ALAssetsLibrary has been deprecated.
ALAssetsLibrary已弃用。
This is the way you should do it:
你应该这样做:
#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);
}
}];
#1
85
I finally found out the answer. Apparently the UIImage methods strip out metadata and so using UIImageWriteToSavedPhotosAlbum is no good.
我终于找到了答案。显然,UIImage方法去掉了元数据,所以使用UIImageWriteToSavedPhotosAlbum不是好方法。
However in ios4 Apple put in a new framework to handle the photo library called the ALAssetsLibrary.
然而,在ios4中,苹果加入了一个新的框架来处理名为ALAssetsLibrary的照片库。
First you need to right click on the Targets and in the build part, add the AlAsset Framework to your project with the little + icon in the bottom left.
首先,您需要右键单击目标,在构建部分,将AlAsset框架添加到项目中,在左下角有一个+图标。
Then add #import "AssetsLibrary/AssetsLibrary.h";
to the header file of your class.
然后添加#进口“AssetsLibrary / AssetsLibrary.h”;到类的头文件。
Finally you can use the following code:
最后,您可以使用以下代码:
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];
And that gets the path of the file you just saved.
得到刚才保存的文件的路径。
#2
3
My code is
我的代码是
-(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];
}
}
in .h import Library and define type def of ALAssetsLibraryWriteImageCompletionBlock
在.h导入库中定义ALAssetsLibraryWriteImageCompletionBlock的类型def
#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>
typedef void (^ALAssetsLibraryWriteImageCompletionBlock)(NSURL *assetURL, NSError *error);
if you don't know how to get <AssetsLibrary/AssetsLibrary.h>
, please add existing framework (AssetsLibrary.framework)
如果您不知道如何获得
#3
1
Swift Version will be
斯威夫特版本将
ALAssetsLibrary().writeImageToSavedPhotosAlbum(editedImage.CGImage, orientation: ALAssetOrientation(rawValue: editedImage.imageOrientation.rawValue)!,
completionBlock:{ (path:NSURL!, error:NSError!) -> Void in
print("\(path)")
})
And "import ALAssetsLibrary" in your file.
和“import ALAssetsLibrary”在您的文件中。
Project-> Build Phases -> Link binary -> AssetsLibrary.framework
项目->构建阶段->链接二进制-> AssetsLibrary.framework
#4
0
- (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);
}
#5
0
ALAssetsLibrary has been deprecated.
ALAssetsLibrary已弃用。
This is the way you should do it:
你应该这样做:
#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);
}
}];