的UIImageView。缩放和居中到任意矩形。无法在屏幕上确定正确的中心 [英] UIImageView. Zoom and Center to arbitrary rectangle. Can't determine correct center on screen

查看:74
本文介绍了的UIImageView。缩放和居中到任意矩形。无法在屏幕上确定正确的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有简单的iPad应用程序。视图层次结构是:
窗口 - UIView - UIImageView
UIImageView加载的图像大于屏幕尺寸(即1280 x 2000对768 x 1004)。我需要放大图像的任意矩形部分(将其放在屏幕上),然后将其置于屏幕中心。目前我按照以下方式放大:

There is simple iPad application. Views hierarchy is: Window - UIView - UIImageView UIImageView has loaded image which is bigger then screen size (i.e. 1280 x 2000 against 768 x 1004). I need to zoom in arbitrary rectangular part of image (fit it on the screen) and then center it on the screen. At the moment I do zooming in following way:

-(IBAction)posIt:(id)sender
{
    // rectangle size and origin in image's coordinates. Its received from out of application
    CGFloat x = 550.f;
    CGFloat y = 1006.f;
    CGFloat w = 719.f;
    CGFloat h = 850.f;

    CGSize frameSize = CGSizeMake(_imgView.image.size.width, _imgView.image.size.height);
    _imgView.frame = CGRectMakeWithParts(CGPointMake(0, 0), frameSize);

    // Calculate center manually because orientation changing
    CGPoint superCenter = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);

    // delta is the factor between size of image size and device screen
    CGFloat delta = 768.f/w;

    // resizing UIImageView to fit rectangle on the screen
    frameSize = CGSizeMake(_imgView.image.size.width * delta, _imgView.image.size.height * delta);
    _imgView.frame = CGRectMakeWithParts(CGPointMake(0, 0), frameSize);

    // Here is I'm trying to calculate center of rectangle
    _imgView.center = CGPointMake(superCenter.x + (superCenter.x - (w/2)*delta - x * delta), superCenter.y + (superCenter.y - (h/2)*delta - y * delta));
}

UIImageView的ContentMode参数是AspectFit。我相信将一个点(UIImageView的中心)移动到另一个点(为了将矩形移动到superView的中心),我将使用以下公式:

UIImageView's ContentMode parameter is AspectFit. I believe that to move one point (UIImageView's center) to another one (in order to move rectangle to the center of the superView) I shall use following formula:

newX = oldX + oldX - rectCenterX;

newY = oldY + oldY - rectCenterY;

由于UIImageView是使用 delta 因子缩放的,因此 rectCenterX rectCenterY 应乘以相同的 delta 的。但我错了。矩形中心从superView中心跳出。

Since UIImageView is scaled with delta factor, rectCenterX and rectCenterY should be multiplied with same delta. But I'm wrong. Rectangles centers jump out from superView center.

是否有某种方法可以正确地进行这些计算?我错过了什么?

Is there exists some way to make these calculations correctly? What do I miss?

提前致谢。

推荐答案

我已经管理了正确和稳定的图像居中。而不是计算图像的中心,我正在计算UIImageView的框架的原点坐标,如下所示:

I've managed correct and stable image centering. Instead of calculating center of image I'm calculating UIImageView's frame's origin coordinates like this:

CGPoint newOrigin = CGPointMake((-zoomRect.origin.x * delta) + superCenter.x - (zoomRect.size.width / 2) * delta, 
                                (-zoomRect.origin.y * delta) + superCenter.y - (zoomRect.size.height / 2) * delta);

事实证明,计算中心和原点之间存在一些差异。无论如何问题都解决了。谢谢大家。

It turns out there is some difference between calculating center and origin. Anyway problem is solved. Thanks everybody.

这篇关于的UIImageView。缩放和居中到任意矩形。无法在屏幕上确定正确的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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