的UIImageView。 缩放和居中任意矩形。 无法在屏幕上确定正确的中心

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

-(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的中心),我将使用下面的公式:

newX = oldX + oldX – rectCenterX;

newY = oldY + oldY – rectCenterY;

由于UIImageView按照增量因子进行缩放,因此rectCenterXrectCenterY应该乘以相同的增量 。 但是我错了。 矩形中心从superView中心跳出。

是否存在一些方法来正确地进行这些计算? 我错过了什么?

提前致谢。

我pipe理正确和稳定的图像居中。 我正在计算UIImageView的框架的原点坐标,而不是计算图像的中心:

 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); 

原来计算中心和原点有一些区别。 无论如何,问题解决了。 谢谢大家。