iOS get Visible portion of Image for an UIImageView
Asked Answered
H

2

3

I'm trying get the visible portion of UIImage from an UIImageView. UIImageView takes the entire screen. Pinch & Pan gesture is added to UIImageView. So, User can pan/zoom the imageview. After pan/zoom I want to crop only the visible part of the image view. I've tried a number of methods. But it returns me wrong part of image.

Thanks In advance.

Hickman answered 20/6, 2012 at 13:34 Comment(1)
Please share your cropping code, thanksDoud
H
1

Thank you all. I've figured out the solution. Here is the code that I used to get the visible portion of the image. (Keep it in your mind that the image was not fully visible when zoomed or pan due to it was taking entire screen)

  - (UIImage *)cropVisiblePortionOfImageView:(UIImageView *)imageView {

  CGFloat zoomScaleX=imageView.frame.size.width/initialWidth;
  CGFloat zoomScaleY=imageView.frame.size.height/initialHeight;
  CGSize zoomedSize=CGSizeMake(initialWidth*zoomScaleX,initialHeight*zoomScaleY);

  UIGraphicsBeginImageContext(zoomedSize);
  [imageView.image drawInRect:CGRectMake(0, 0, zoomedSize.width, zoomedSize.height)];
  UIImage *zoomedImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  UIGraphicsBeginImageContext(CGSizeMake(initialWidth, initialHeight));
  [zoomedImage drawAtPoint:CGPointMake(imageView.frame.origin.x, imageView.frame.origin.y)];
  UIImage *cropedImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  return cropedImage;
}
Hickman answered 21/6, 2012 at 11:18 Comment(1)
What is initialWidth & initialHeight ?Tramel
N
11

Include QuartzCore framework to your project and implement this method for getting visible part of image from your image view: (one note: i mean that you are using ARC in your project)

#import <QuartzCore/QuartzCore.h>

- (UIImage*)imageFromImageView:(UIImageView*)imageView
{
    UIGraphicsBeginImageContext(imageView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextRotateCTM(context, 2*M_PI);

    [imageView.layer renderInContext:context];
    UIImage *image =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
Nabala answered 20/6, 2012 at 14:42 Comment(1)
Thanks a lot #DizAsm. It works perfectly for my imageview. I have set below property for my imageview.[imageView setClipsToBounds:YES]; and imageView.contentMode = UIViewContentModeScaleAspectFill. It was very difficult to get exact image from imageview visible area. Now it works perfectly.Entitle
H
1

Thank you all. I've figured out the solution. Here is the code that I used to get the visible portion of the image. (Keep it in your mind that the image was not fully visible when zoomed or pan due to it was taking entire screen)

  - (UIImage *)cropVisiblePortionOfImageView:(UIImageView *)imageView {

  CGFloat zoomScaleX=imageView.frame.size.width/initialWidth;
  CGFloat zoomScaleY=imageView.frame.size.height/initialHeight;
  CGSize zoomedSize=CGSizeMake(initialWidth*zoomScaleX,initialHeight*zoomScaleY);

  UIGraphicsBeginImageContext(zoomedSize);
  [imageView.image drawInRect:CGRectMake(0, 0, zoomedSize.width, zoomedSize.height)];
  UIImage *zoomedImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  UIGraphicsBeginImageContext(CGSizeMake(initialWidth, initialHeight));
  [zoomedImage drawAtPoint:CGPointMake(imageView.frame.origin.x, imageView.frame.origin.y)];
  UIImage *cropedImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  return cropedImage;
}
Hickman answered 21/6, 2012 at 11:18 Comment(1)
What is initialWidth & initialHeight ?Tramel

© 2022 - 2024 — McMap. All rights reserved.