2

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

谢谢

4

1 回答 1

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

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

于 2010-01-12T02:39:07.963 回答