-1

簡単なiPadアプリがあります。ビューの階層は次のとおりです。ウィンドウ - UIView - UIImageView UIImageView は、画面サイズよりも大きいイメージをロードしました (つまり、768 x 1004 に対して 1280 x 2000)。画像の任意の長方形の部分を拡大して(画面に合わせて)、画面の中央に配置する必要があります。現時点では、次の方法でズームを行っています。

-(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の中心)を別のポイントに移動するには(四角形をスーパービューの中心に移動するため)、次の式を使用する必要があると思います:

newX = oldX + oldX - rectCenterX;

newY = oldY + oldY - rectCenterY;

UIImageView はデルタ係数でスケーリングされるため、rectCenterXrectCenterYは同じdeltaで乗算する必要があります。しかし、私は間違っています。長方形の中心がスーパービューの中心から飛び出します。

これらの計算を正しく行う方法はありますか? 何が恋しいですか?

前もって感謝します。

4

1 に答える 1

0

正しく安定した画像のセンタリングを管理しました。画像の中心を計算する代わりに、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);

中心と原点の計算には多少の違いがあることがわかります。とにかく問題は解決しました。みんなありがとう。

于 2012-10-02T22:35:21.923 に答える