IOS get visible image part for UIImageView

I am trying to get the visible part of a UIImage from a UIImageView . UIImageView displays full screen. The transponder gesture and gestures are added to the UIImageView . Thus, the user can pan / enlarge the image. After panning / zooming, I want to crop only the visible part of the image. I tried several methods. But he returns me the wrong part of the image.

Thanks in advance.

+1
ios objective-c iphone
Jun 20 2018-12-12T00:
source share
2 answers

Thanks to everyone. I figured out a solution. Here is the code I used to get the visible part of the image. (Keep in mind that the image was not fully visible when zoomed in or panned due to the fact that it was shooting the 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; } 
+1
Jun 21 '12 at 11:18
source share

Include the QuartzCore framework in your project and implement this method to get the visible part of the image from your image: (one note: I mean that you use 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; } 
+9
Jun 20 2018-12-12T00:
source share



All Articles