1

In my application, users can add UIImageView as subview of my UIScrollView. When the user taps "Save" button, I want to save all the content of my UIScrollView as an UIImage into the photo library.

I've been looking on Google but there is not much on the subject.

Edit: the below code does the job

        CGRect rect = self.mainView.frame;
        [self.mainView setFrame:CGRectMake(0, 0, 824, self.mainView.contentSize.height)];
        UIGraphicsBeginImageContext(self.mainView.bounds.size);
        [self.mainView.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        UIImageWriteToSavedPhotosAlbum(image , nil, nil, nil);
        [self.mainView setFrame:rect];

1 Answer 1

2

Seems like you need to take screen shot of screen, you can try out this code on done action -

UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:@"myImage.png" atomically:YES];
4
  • Don't forget to resize the scroll view to fit the content before rendering, then resizing it back to its original size.
    – Léo Natan
    May 12, 2012 at 16:39
  • 1
    I got this error with the second line: Receiver type 'CALayer' for instance message is a forward declaration
    – Benjamin
    May 12, 2012 at 16:41
  • 1
    add this - #import <QuartzCore/QuartzCore.h>
    – rishi
    May 12, 2012 at 16:42
  • Thanks a lot rishi and Leo Natan
    – Benjamin
    May 12, 2012 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.