根据UIScrollView内部的视口裁剪UIImage [英] Cropping a UIImage according to the viewport inside a UIScrollView
问题描述
我在UIScrollView中有一个UIImageView(用Aspect Fill初始化)。
用户应使用它来调整UIImage的大小并将其定位到自己喜欢的位置,按下按钮,然后将他在屏幕上看到的确切内容仅在原始图像坐标中发送到服务器。换句话说,生成的图像将大于屏幕上的图像。
I have a UIImageView (initialized with Aspect Fill) inside a UIScrollView. The user shall use that to resize and position a UIImage to his like, press a button and then exactly what he sees on screen would be sent to a server, only in the original image coordinates. In other words, the resulting image would be bigger than the image on screen.
到目前为止,这是我的代码的必要部分:
Here's the essential part of my code so far:
// select the original
CGFloat imageRatio = self.imageView.image.size.width / self.imageView.image.size.height;
CGFloat viewRatio = self.imageView.frame.size.width / self.imageView.frame.size.height;
CGFloat scale;
// get the scale factor of the image being "aspect-filled"
if(imageRatio < viewRatio) {
scale = self.imageView.image.size.width / self.imageView.frame.size.width;
} else {
scale = self.imageView.image.size.height / self.imageView.frame.size.height;
}
// get the cropping origin in the image's original coordinates
CGFloat originX = self.imageScrollView.contentOffset.x * scale;
CGFloat originY = self.imageScrollView.contentOffset.y * scale;
CGPoint origin = CGPointMake(originX, originY);
// get the cropping size in the image's original coordinates
CGFloat width = self.imageView.image.size.width / self.imageScrollView.zoomScale;
CGFloat height = self.imageView.image.size.height / self.imageScrollView.zoomScale;
CGSize size = CGSizeMake(width, height);
// create the new image from the original one
CGRect imageRect = CGRectMake(origin.x, origin.y, size.width, size.height);
struct CGImage *croppedCGImage = CGImageCreateWithImageInRect(self.imageView.image.CGImage, imageRect);
它看起来已经很接近我想要的了。原点计算至少看起来是正确的。但是,生成的图像仍然存在一些差异,我猜这可能是由于可能的图像的纵横比不同所致。可能需要以某种方式将 scale
添加到我的 size
等式中,但我真的无法将自己的头围住如何。
It already looks quite close to what I want. The origin calculation at least seems correct. However, there is still some difference in the resulting image, which I guess comes from the different aspect ratios of the possible images. Probably I need to add scale
to my size
equations somehow, but I seriously just can't wrap my head around how.
希望有人能帮助我把这两米弄完...
Hopefully somebody can help me get this last two meters...
根据第一个评论,我尝试了以下方法。但是以某种方式,我的 targetFrame
始终与 scrollFrame
相同。对我来说,现在晚上做点正确的事情太晚了吗?
According to the first comment, I tried out the following. But somehow my targetFrame
is always identical to scrollFrame
. Is it simply too late at night for me to get something right??
CGFloat zoom = self.imageScrollView.zoomScale;
// the full image resolution
CGRect fullImageFrame = CGRectMake(0, 0,
self.imageView.image.size.width,
self.imageView.image.size.height);
UIView *fullImageView = [[[UIView alloc] initWithFrame:fullImageFrame] autorelease];
// the theoretical size of the image on the screen
CGRect previewImageFrame = CGRectMake(0, 0,
self.imageView.frame.size.width * zoom,
self.imageView.frame.size.height * zoom);
UIView *previewImageView = [[[UIView alloc] initWithFrame:previewImageFrame] autorelease];
// the excerpt of previewImageFrame really visible
CGRect scrollFrame = CGRectMake(self.imageScrollView.contentOffset.x,
self.imageScrollView.contentOffset.y,
self.imageScrollView.frame.size.width,
self.imageScrollView.frame.size.height);
// that excerpt transferred to the full resolution image's coordinates
CGRect targetFrame = [fullImageView convertRect:scrollFrame fromView:previewImageView];
推荐答案
您可能希望探索:
(CGRect)convertRect:(CGRect)rect toView:(UIView *)view
通过这种方法和其他类似方法,您可以基于视图的位置并调整其坐标系,而不是将其放置在原来的视图中。
This and other similar methods allow you to take a view and adjust its coordinate system based on placement in a superview other than the one it was initially intended or positioned for.
这篇关于根据UIScrollView内部的视口裁剪UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!