View Full Version : How do I get an image from a scroll view

May 4, 2009, 03:50 AM
I have a UIScrollView, containing a UIImage view, which contains an image from the camera. Once the user has finished scrolling/zooming etc, I want to save just the visible portion of the image.
Sounds simple really. But can I do it? Nope.

I've tried all combinations of multiplying content offsets and scroll view bounds by the scaling factor and who knows what else into a CGRect that I use to extract theimage using CGImageCreateWithImageInRect, but I never get the right image.

Sample code:
CGRect imageFrame = CGRectMake(scrollView.contentOffset.x,
scrollView.bounds.size.width * scrollViewZoom,
scrollView.bounds.size.height * scrollViewZoom);

CGImageRef ref = CGImageCreateWithImageInRect([imageView.image CGImage], imageFrame);

// draw this image onto another
myImage = [UIImage imageWithCGImage:ref];

Help! Does anyone have any working example code, or at least a nudge in the right direction to help a newbie developer on his way.

Many thanks in advance!

May 4, 2009, 09:28 AM
Maybe you need to divide by the zoom, not multiply to get the width and height.