在UIScrollView内获取UIImage可见部分的正确坐标 [英] Getting the right coordinates of the visible part of a UIImage inside of UIScrollView

查看:80
本文介绍了在UIScrollView内获取UIImage可见部分的正确坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UIScrollView内有一个UIImage,因此可以放大和缩小,裁剪,四处移动等.

I have a UIImage which is inside a UIScrollView so I can zoom-in and zoom-out, crop, move around, etc.

如何获取UIScrollView中UIImage可见部分的坐标. 我想以原始分辨率(使用GPUIImage)裁剪图像,但是我需要为矩形设置x,y,宽度和高度.

How do I get the coordinates of the visible part of the UIImage inside the UIScrollView. I want to crop the image in it's native resolution (using GPUIImage) but I need to x, y, width and height for the rectangle.

推荐答案

我使用滚动视图启用UIImage的缩放.裁剪按钮显示可调整大小的叠加裁剪视图.以下是我用来确保将UIScrollView中用户定义的裁剪框添加到UIImageView的正确坐标的代码(然后可以用于裁剪图像).

I use a scrollview to enable zoom of a UIImage. A cropping button presents an overlaid resizable cropping view. The following is the code I use to ensure the user defined crop box in the UIScrollView gets added with the correct coordinates to the UIImageView (can then use to crop image).

要找到x和y坐标,请使用scrollview contentOffset乘以zoomscale的倒数:

To find the x and y coordinates use the scrollview contentOffset multiplied by inverse of zoomscale:

float origX = 0;
float origY = 0;
float widthCropper = 0;
float heightCropper = 0;


if (_scrollView.contentOffset.x > 0){
    origX = (_scrollView.contentOffset.x) * (1/_scrollView.zoomScale);
}
if (_scrollView.contentOffset.y > 0){
    origY = (_scrollView.contentOffset.y) * (1/_scrollView.zoomScale);
}

如果您需要为滚动视图中显示的内容创建适当大小的裁剪框,则需要针对任何缩放系数调整宽度和高度:

If you need to create a properly sized cropping box for what is displayed in the scrollview you will need to adjust the width and the height for any zoom factor:

widthCropper = (_scrollView.frame.size.width * (1/_scrollView.zoomScale));
heightCropper = (_scrollView.frame.size.height * (1/_scrollView.zoomScale));

并将此尺寸适当的矩形作为裁剪视图添加到UIImageView:

and to add this properly sized rectangle as a cropping view to the UIImageView:

CGRect cropRect = CGRectMake((origX + (SIDE_MARGIN/2)), (origY + (SIDE_MARGIN / 2)), (widthCropper - SIDE_MARGIN), (heightCropper  - SIDE_MARGIN));
_cropView = [[UIView alloc]initWithFrame:cropRect];
[_cropView setBackgroundColor:[UIColor grayColor]];
[_cropView setAlpha:0.7];
[_imageView addSubview:_cropView];
[_imageView setCropView:_cropView];

这篇关于在UIScrollView内获取UIImage可见部分的正确坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆