iOS为UIImageView获取图像的可见部分

我想从UIImageViewUIImage的可见部分。 UIImageView占用整个屏幕。 捏和平手势被添加到UIImageView 。 所以,用户可以平移/缩放imageview。 平移/缩放后,我只想裁剪图像视图的可见部分。 我已经尝试了一些方法。 但它回报我错了图像的一部分。

提前致谢。

在你的项目中包含QuartzCore框架,并实现这个方法来从你的图像视图中获得可见的图像部分:(注意:我的意思是你在你的项目中使用ARC)

 #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; } 

谢谢你们。 我已经想出了解决scheme。 这是我用来获取图像可见部分的代码。 (请记住,缩放或平移时,图像不完全可见,因为它正在全屏显示)

  - (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; }