1

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

4

3 回答 3

1

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

// 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);
}
于 2012-07-11T11:16:19.727 回答
0

使用以下:

- (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];
}

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

于 2012-07-11T11:13:31.840 回答
0

声明一个整数 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];
于 2012-07-11T12:18:21.503 回答