如何保存UIImagePickerView控制器返回的图像 [英] How to save an image that is returned by UIImagePickerView Controller

查看:94
本文介绍了如何保存UIImagePickerView控制器返回的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,用户将从 UIImagePickerView 中选择一个图像。

I have an application, in which the user will select an image from a UIImagePickerView.

如何实现?

感谢您提前帮助我。

推荐答案

我会这样说:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
self.resumePreviousSettingAfterEditing = true;
[self.topImageView setImage:image];
[cubeModel setImage:image forFace:[cubeModel.validFaces objectAtIndex:selectedRowInFacePicker]];

[self dismissImagePickerAnimated:true];

}

你的控制器来处理图像选择。在该事件处理程序中,调用某处的某个方法,例如在您的模型中设置新的图像。该函数看起来像这样:

You register an event in your controller to handle the image selection. In that event handler, call a method somewhere, say in your model to set the new image. That function would look something like this:

 (void)saveImage:(UIImage *)image withName:(NSString *)imageName {
    // get the image path
    NSString *filename = [self determineImagePath:imageName];

    // make sure the file is removed if it exists
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:filename]) {
        if(NO == [fileManager removeItemAtPath:filename error:NULL]) {
        }
    }

    // Now, save the image to a file.
    if(NO == [UIImagePNGRepresentation(image) writeToFile:filename atomically:YES]) {
        [NSException raise:@"Image Save Failed" format:@"Unable to store image %s", filename]; 
    }
}

当您想再次加载图片时,所以像:

When you want to load the image again, you would so something like:

- (UIImage *)loadImage:(NSString *)imageName {
    NSString *filename = [self determineImagePath:imageName];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    self.currentImage = nil;
    if([fileManager fileExistsAtPath:filename]) {

        NSData *imageData = [[NSData alloc] initWithContentsOfFile:filename];
        UIImage *image = [UIImage imageWithData:imageData];

        self.currentImage = image;
    }
    return self.currentImage;
}

不要让我开始转型, be。

And don't get me started on transforming which is way harder than it should be.

享受,
雅各布

Enjoy, Jacob

这篇关于如何保存UIImagePickerView控制器返回的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆