Jay Chauhan
Jay Chauhan

Reputation: 99

Saving an image displayed in a UIImageView

I have an array of images that are displayed in a UICollectionView.

When a cell in the collection view is pressed, that image is pushed to a view controller and displayed in a UIImageView.

I want to then be able to press a button and save the image to the users camera roll.

But I'm having some trouble doing so...

I think I'm on the right lines with the code but can't get it all to work together:

- (IBAction)onClickSavePhoto:(id)sender{

     UIImage *img = [UIImage imageNamed:@"which ever image is being currently displayed in the image view"];

     UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
}

How can i manipulate the code to allow the user to save the image displayed in the image view?

Thanks in advance!

UPDATE:

Found the solution to the problem in another post:

Save image in UIImageView to iPad Photos Library

Upvotes: 1

Views: 965

Answers (1)

Pavan
Pavan

Reputation: 18548

How to save an image to the library:

You can use this function:

UIImageWriteToSavedPhotosAlbum(UIImage *image, 
                               id completionTarget, 
                               SEL completionSelector, 
                               void *contextInfo);

You only need completionTarget, completionSelector and contextInfo if you want to be notified when the UIImage is done saving, otherwise you can pass in nil.

More info here

Supposedly a faster way to save an image to the library than using UIImageWriteToSavedPhotosAlbum: There`s much more fast then UIImageWriteToSavedPhotosAlbum way to do it using iOS 4.0+ AVFoundation framework

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
    if (error) { // TODO: error handling } 
    else { // TODO: success handling }
}];

//for non-arc projects
//[library release];

Get image of whatever is in the UIImageView as a screenshot:

iOS 7 has a new method that allows you to draw a view hierarchy into the current graphics context. This can be used to get an UIImage very fast.

This is a category method on UIView to get the view as an UIImage:

- (UIImage *)takeSnapShot {
    UIGraphicsBeginImageContextWithOptions(self.myImageView.bounds.size, NO, [UIScreen mainScreen].scale);

    [self drawViewHierarchyInRect:self.myImageView.bounds afterScreenUpdates:YES];

    // old style [self.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

It is considerably faster then the existing renderInContext: method.

Reference : https://developer.apple.com/library/ios/qa/qa1817/_index.html

UPDATE FOR SWIFT: An extension that does the same:

extension UIView {

    func takeSnapshot() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.myImageView.bounds.size, false, UIScreen.mainScreen().scale);

        self.drawViewHierarchyInRect(self.myImageView.bounds, afterScreenUpdates: true)

        // old style: self.layer.renderInContext(UIGraphicsGetCurrentContext())

        let image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
}

Upvotes: 2

Related Questions