Josh Kahane
Josh Kahane

Reputation: 17159

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.

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.

Upvotes: 1

Views: 1853

Answers (1)

scooter133
scooter133

Reputation: 1317

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.

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.

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

}

Upvotes: 3

Related Questions