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

时间:2010-01-12 01:32:51

标签: iphone uiimagepickercontroller

我使用UIPickerController从iphone捕获图像,然后以编程方式使用特定名称保存图像,以及稍后调用图像(使用名称),是否可以更改图像大小,例如。从100 x 100像素到50 x 50像素

感谢

1 个答案:

答案 0 :(得分:7)

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

- (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. 
    }
}

然后从文件中调用图像:

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

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