iOS - 是否可以在将图像文件从应用程序保存到 iPhone 设备库之前重命名它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22686561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS - Is it possible to rename image file name before saving it into iPhone device gallery from app?
提问by user2786
Hey I'm new to iPhone and I have been trying to make an gallery kind of app. Basically, what I want to do is that i need to save all the captured images into a specific folder like a new album "My_App Images" related to our app name in iPhone device gallery, it's working for me, but I am having trouble to change the image file name, i don't know that Is it possible to specify a file name? Using iPhoto, currently i am getting image file name as "IMG_0094.jpg", can we change it with any other file name like "Anyfilename.png" format programmatically?
嘿,我是 iPhone 的新手,我一直在尝试制作一个画廊类的应用程序。基本上,我想要做的是,我需要将所有捕获的图像保存到一个特定的文件夹中,例如与我们在 iPhone 设备库中的应用程序名称相关的新专辑“My_App Images”,它对我有用,但我遇到了麻烦更改图像文件名,我不知道是否可以指定文件名?使用 iPhoto,目前我得到的图像文件名为“IMG_0094.jpg”,我们可以用任何其他文件名(如“Anyfilename.png”)以编程方式更改它吗?
here is my code for saving images to the specific album :
这是我将图像保存到特定相册的代码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
[self.library saveImage:image toAlbum:@"My_App Images" withCompletionBlock:^(NSError *error) {
if (error!=nil) {
NSLog(@"Image saving error: %@", [error description]);
}
}];
[picker dismissViewControllerAnimated:NO completion:nil];
}
Any source or link for reference is appreciated. Thanks for the help!
任何供参考的来源或链接表示赞赏。谢谢您的帮助!
回答by Yonat
There is a way to kinda do that, by setting the image IPTC metadata field "Object Name". If you later import the image to iPhoto, then this name will be used as its title.
有一种方法可以做到这一点,通过设置图像 IPTC 元数据字段“对象名称”。如果您稍后将图像导入 iPhoto,则此名称将用作其标题。
See details (and code) at http://ootips.org/yonat/how-to-set-the-image-name-when-saving-to-the-camera-roll/.
在http://ootips.org/yonat/how-to-set-the-image-name-when-saving-to-the-camera-roll/ 上查看详细信息(和代码)。
回答by Fahim Parkar
Do you meant,
你的意思是,
// Build NSData in memory from the btnImage...
NSData* imageData = UIImageJPEGRepresentation(image, 1.0);
// Save to the default Apple (Camera Roll) folder.
[imageData writeToFile:@"/private/var/mobile/Media/DCIM/100APPLE/customImageFilename.jpg" atomically:NO];
Now adjust the path of folder as per your folder name...
现在根据您的文件夹名称调整文件夹的路径...
回答by Anindya Sengupta
Sorry to disappoint you, but it seems that you can not change the name of the photos, before or after saving, in the photo album, custom or not. Here is a post to explain it:
很抱歉让您失望了,但似乎您无法更改照片名称,保存之前或之后,相册中,自定义与否。这是一个解释它的帖子:
iOS rename/delete albums of photos
Edit
编辑
So, to clarify my comment, use the following override:
因此,为了澄清我的评论,请使用以下覆盖:
- Download the NSMutableDictionary category for metadata of image here.
Also download the sample project
CustomAlbumDemo
from hereand modify theNSMutableDictionary+ImageMetadata.m
file in theCustomAlbumDemo
project as:-(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock { //write the image data to the assets library (camera roll) NSData* imageData = UIImageJPEGRepresentation(image, 1.0); NSMutableDictionary *metadata = [[NSMutableDictionary alloc] init]; [metadata setDescription:@"This is my special image"]; [self writeImageDataToSavedPhotosAlbum:imageData metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error) { //error handling if (error!=nil) { completionBlock(error); return; }
//add the asset to the custom photo album [self addAssetURL: assetURL toAlbum:albumName withCompletionBlock:completionBlock]; }];
}
- 在此处下载图像元数据的 NSMutableDictionary 类别。
也
CustomAlbumDemo
从这里下载示例项目NSMutableDictionary+ImageMetadata.m
并将CustomAlbumDemo
项目中的文件修改为:-(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock { //write the image data to the assets library (camera roll) NSData* imageData = UIImageJPEGRepresentation(image, 1.0); NSMutableDictionary *metadata = [[NSMutableDictionary alloc] init]; [metadata setDescription:@"This is my special image"]; [self writeImageDataToSavedPhotosAlbum:imageData metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error) { //error handling if (error!=nil) { completionBlock(error); return; }
//add the asset to the custom photo album [self addAssetURL: assetURL toAlbum:albumName withCompletionBlock:completionBlock]; }];
}