views:

465

answers:

1

Hi

I am using a UIImageview inside a UIScrollView to let users pinch and zoom the image. I now want to get the image which the user has modified (small or zoomed in). How can I do this?

Thanks.

OK, am trying something new now. Can I get a thumbnail from an image from somewhere in between? I can get the thumbnail from (0,0) for a fixed size. Can I get it from say (10, 20) or (30, 30)? Thanks again. I tried using CGImageCreateWithImageInRect with a rect having these values for x and y. But it gives me an image from point 0,0

+1  A: 

When you zoom UIImageView object inside UIScrollView, you change only UIImageView frame, but UIImage in image property remains unchanged.

if you only want to show this image scaled in some other UIImageView, you should not change UIImage size, just set your new UIImageView frame same as frame of zoomed UIImageView from UIScrollView and use same image:

UIImageView * ImageViewOnScroll;
//set image and zoom it
... 
UIImageView * newImageView = [[UIImageView alloc] initWithImage:ImageViewOnScroll.image];
newImageView.frame = ImageViewOnScroll.frame;

if for some reason you want to create new UIImage with changed size you can do it for example with following simple method:

UIImage * resizeImage(UIImage * img, CGSize newSize){
    UIGraphicsBeginImageContext(newSize);

    //or other CGInterpolationQuality value
    CGContextSetInterpolationQuality(UIGraphicsGetCurrentContext(), kCGInterpolationDefault);

    [img drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
Vladimir
AFAIK the method you have mentioned (resizeImage) will only give me a part of the image cropped. It will not give me the zoomed image.
lostInTransit
Thanks vladd, gave me an idea for achieving my purpose. Please see updated question.
lostInTransit
above code (resizeImage method) returns whole image resized. To take part of the original image use CGImageCreateWithImageInRect(CGImageRef image, CGRect rect);Combining both CGImageCreateWithImageInRect and drawing on context you can take any part of image, resize it to any size, and place it within any region of new image.
Vladimir