How do I get an image from a scroll view

Discussion in 'iOS Programming' started by JimBobBennett, May 4, 2009.

  1. macrumors member

    May 4, 2009
    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!
  2. macrumors 68030


    Sep 2, 2008
    Maybe you need to divide by the zoom, not multiply to get the width and height.

Share This Page