UIImagePickerController - 保存并加载图像?

时间:2011-10-19 22:24:29

标签: iphone objective-c load save uiimagepickercontroller

关于这个主题似乎有一些信息分散,但这看起来很混乱,所以我锄你不介意我问。

我有一个UIImagePickerController工作,让我拍照或从库中选择,然后设置我选择的UIImageView来显示拍摄/选择的图像。

但是,如何首先保存图像,然后在重新打开应用程序时将其重新加载?此外,我将保存多个图像,因此需要进行唯一标识。

非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:3)

我不知道第二次从相册中获取照片的方法。似乎没有办法再次检索它。我们做了一些事情来内部保存图像。

我们要么将Image转换为NSData并将其保存在CoreData中,要么将其保存到我们的沙盒中。

这里是我们使用的一段代码,这是做一些事情之一,我从ScreenShot获取图像,虽然它与UIImage一样。

    // go get our screenshot
UIImage* screenShot = [self createScreenShotThumbnailWithWidth:200];
    // Save screen shot as a png in the documents directory with the UUID of the IoCDScreen
    // Saving to Sandbox
     [self saveImage:screenShot withName:currentScreen.itemUUID];

    // save image to Photo Album - though you can't get a ref back to it
    UIImageWriteToSavedPhotosAlbum(screenShot, self, nil, nil);

    //convert our screen shot PNG to NSData and store it in a CoreData Managed Object
currentScreen.screenImageDevelopment =  [NSData dataWithData: UIImagePNGRepresentation( screenShot )];

上面使用的助手功能

    //--------------------------------------------------------------------------------------------------------
    //    saveImage:withName:
    //    Description: Save the Image with the name to the Documents folder
    //
    //--------------------------------------------------------------------------------------------------------

- (void)saveImage:(UIImage *)image withName:(NSString *)name {

        //grab the data from our image
    NSData *data = UIImageJPEGRepresentation(image, 1.0);

        //get a path to the documents Directory
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,  YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

        // Add out name to the end of the path with .PNG
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", name]];

        //Save the file, over write existing if exists. 
    [fileManager createFileAtPath:fullPath contents:data attributes:nil];

}

    //--------------------------------------------------------------------------------------------------------
    //    createScreenShotThumbnailWithWidth
    //    Description: Grab a screen shot and then scale it to the width supplied
    //
    //--------------------------------------------------------------------------------------------------------

-(UIImage *) createScreenShotThumbnailWithWidth:(CGFloat)width{
        // Size of our View
    CGSize size = self.view.bounds.size;

        //First Grab our Screen Shot at Full Resolution
    UIGraphicsBeginImageContext(size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();


        //Calculate the scal ratio of the image with the width supplied.
    CGFloat ratio = 0;
    if (size.width > size.height) {
        ratio = width / size.width;
    } else {
        ratio = width / size.height;
    }

        //Setup our rect to draw the Screen shot into 
    CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);

        //Scale the screenshot and save it back into itself
    UIGraphicsBeginImageContext(rect.size);
    [screenShot drawInRect:rect];
    screenShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

        //Send back our screen shot
    return screenShot;

}