objective-cuiscrollviewuiimagephotolibrary

Convert content of UIScrollView in UIImage


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

Solution

  • 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];