且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用特定名称保存UIImagePicker图像

更新时间:2023-11-30 11:32:28

以下代码片段将图像保存到文件中:

The following code snippet will save the image to a file:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *anImage = [info valueForKey:UIImagePickerControllerOriginalImage];
    NSData *imageData = UIImageJPEGRepresentation(anImage, 1.0);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSString *tmpPathToFile = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/specificImagedName.jpg", path]];

    if([imageData writeToFile:tmpPathToFile atomically:YES]){
          //Write was successful. 
    }
}

然后从文件中调用图像:

And then to recall the image from a file:

NSData *imageData = [NSData dataWithContentsOfFile:tmpPathToFile];
[UIImage imageWithData:imageData];

最后,这篇文章有一个可用的方法,您可以将其合并到项目中以调整图像大小。

Finally, this post has a method available that you can incorporate into your project to resize the image.