如何动态保存图像

时间:2021-06-19 21:24:31

I am using photo library in my app. And after selecting image from photo library or camera i want to save that image into my Documents folder. So here i want to give name to that image while saving is it possible to set name to the selected image? If means please let me know. I am trying that only if possible i will post that.Thank you.

我在我的应用程序中使用照片库。从照片库或相机中选择图像后,我想将该图像保存到我的Documents文件夹中。所以在这里我想给那个图像命名,同时保存是否可以将名称设置为所选图像?如果有意思请告诉我。我正在尝试,只有在可能的情况下,我会发布那个。谢谢。

3 个解决方案

#1


1  

This is how I do it.. I'm copying and pasting my code so there's some additional functionality.

我就是这样做的..我正在复制和粘贴我的代码,所以还有一些额外的功能。

// image is a UIImage
// inputText is the user selected imagename
// date is a string I inserting to make pictures a unique identifier (i.e. no duplicate names)
NSData *imageData1 = UIImageJPEGRepresentation(image, 1.0);
NSString *imageFilename = [NSString stringWithFormat:@"%@-%@.jpg", inputText,date];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *path = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0], imageFilename];

if([imageData1 writeToFile:path atomically:YES]){
    NSLog(@"Write to Document folder success filename = %@",path);
}

#2


0  

Use following :

使用以下:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage *photoImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSString *savedDoc = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/myImageName.png"];
BOOL status = [[NSFileManager defaultManager] fileExistsAtPath:savedDoc];

if(status){

[[NSFileManager defaultManager] removeItemAtPath:savedDoc error:nil];

}
[UIImagePNGRepresentation(photoImage) writeToFile:savedDoc atomically:YES];
[picker dismissModalViewControllerAnimated:YES];
}

You can set any desired name when saving to document directory.

保存到文档目录时,可以设置任何所需的名称。

#3


0  

Declare an integer counterNo and initialize it to 0. And increase its value to change name dynamically.

声明一个整数counterNo并将其初始化为0.并增加其值以动态更改名称。

UIImage *photoImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

counterNo++;

NSString *aaa = [NSString stringWithFormat:@"Documents/myImage-%d.png" , counterNo];
NSString *savedDoc = [NSHomeDirectory() stringByAppendingPathComponent:aaa];
BOOL status = [[NSFileManager defaultManager] fileExistsAtPath:savedDoc];

if(status){

    [[NSFileManager defaultManager] removeItemAtPath:savedDoc error:nil];

}
[UIImagePNGRepresentation(photoImage) writeToFile:savedDoc atomically:YES];
[picker dismissModalViewControllerAnimated:YES];

#1


1  

This is how I do it.. I'm copying and pasting my code so there's some additional functionality.

我就是这样做的..我正在复制和粘贴我的代码,所以还有一些额外的功能。

// image is a UIImage
// inputText is the user selected imagename
// date is a string I inserting to make pictures a unique identifier (i.e. no duplicate names)
NSData *imageData1 = UIImageJPEGRepresentation(image, 1.0);
NSString *imageFilename = [NSString stringWithFormat:@"%@-%@.jpg", inputText,date];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *path = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0], imageFilename];

if([imageData1 writeToFile:path atomically:YES]){
    NSLog(@"Write to Document folder success filename = %@",path);
}

#2


0  

Use following :

使用以下:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage *photoImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSString *savedDoc = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/myImageName.png"];
BOOL status = [[NSFileManager defaultManager] fileExistsAtPath:savedDoc];

if(status){

[[NSFileManager defaultManager] removeItemAtPath:savedDoc error:nil];

}
[UIImagePNGRepresentation(photoImage) writeToFile:savedDoc atomically:YES];
[picker dismissModalViewControllerAnimated:YES];
}

You can set any desired name when saving to document directory.

保存到文档目录时,可以设置任何所需的名称。

#3


0  

Declare an integer counterNo and initialize it to 0. And increase its value to change name dynamically.

声明一个整数counterNo并将其初始化为0.并增加其值以动态更改名称。

UIImage *photoImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

counterNo++;

NSString *aaa = [NSString stringWithFormat:@"Documents/myImage-%d.png" , counterNo];
NSString *savedDoc = [NSHomeDirectory() stringByAppendingPathComponent:aaa];
BOOL status = [[NSFileManager defaultManager] fileExistsAtPath:savedDoc];

if(status){

    [[NSFileManager defaultManager] removeItemAtPath:savedDoc error:nil];

}
[UIImagePNGRepresentation(photoImage) writeToFile:savedDoc atomically:YES];
[picker dismissModalViewControllerAnimated:YES];