iOS为UIImageView获取Image的Visible部分 [英] iOS get Visible portion of Image for an UIImageView
问题描述
我正在尝试从UIImageView
获取UIImage
的可见部分. UIImageView
占据整个屏幕.捏&平移手势已添加到UIImageView
.因此,用户可以平移/缩放图像视图.平移/缩放后,我只想裁剪图像视图的可见部分.我已经尝试了多种方法.但这会返回错误的图像部分.
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.
谢谢.
推荐答案
谢谢大家.我已经找到了解决方案.这是我用来获取图像可见部分的代码. (请记住,由于放大或平移图像占用了整个屏幕,因此图像并不完全可见)
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;
}
这篇关于iOS为UIImageView获取Image的Visible部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!