简体   繁体   English

UIImagePickerController-保存并加载图像?

[英]UIImagePickerController - Save and Load Image?

There seems to be a scatter of info on this topic, but it seems rather a muddle, so I hoe you don't mind me asking. 关于此主题的信息似乎很少,但似乎很混乱,所以我不介意我问。

I have a UIImagePickerController working, letting me either take a photo or pick on from the library and then set a UIImageView of my choice to show that image taken/selected. 我有一个正在工作的UIImagePickerController,让我可以拍照或从库中拾取图片,然后设置自己选择的UIImageView以显示拍摄/选择的图像。

However, how can I firstly save the image, then load it back in when the application is reopened? 但是,如何首先保存图像,然后在重新打开应用程序时将其重新加载? Also, I will be saving multiple images so they need to be uniquely identified. 此外,我将保存多个图像,因此需要进行唯一标识。

Any help will be massively appreciated, thanks. 任何帮助都将受到大力赞赏,谢谢。

I don't know of a way to get the Photo from the Photo Album a Second time. 我不知道第二次从相册获得照片的方法。 There does not seem to be a way to retrieve it again. 似乎没有办法再次检索它。 We do a few things to save the Image internally. 我们做了一些事情来内部保存图像。

We either convert the Image to NSData and save it in CoreData, or we save it off to our sandbox. 我们将Image转换为NSData并将其保存在CoreData中,或者将它保存到我们的沙箱中。

here is a snip of code that we use, this is doing one of a few things, I'm getting the Image from a ScreenShot, though its the same once you have the UIImage. 这是我们使用的一小段代码,它是在做以下几件事之一,我从ScreenShot获取了Image,尽管一旦拥有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 )];

Helper Functions used above 上面使用的助手功能

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM