CrazyDev CrazyDev - 2 months ago 13
iOS Question

CGRect to UIImage

I have this code:

CGRect visibleRect;
visibleRect.origin = mainViewController.scrollView.contentOffset;
visibleRect.size = mainViewController.scrollView.bounds.size;


I want to store the content inside
visibleRect
in a
UIImage
. Is that possible?

Answer

This code may work:

UIGraphicsBeginImageContext(visibleRect.size);
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), visibleRect.origin.x,
    visibleRect.origin.y);
[mainViewController.scrollView.layer 
    renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Comments