1

UIViewの境界を変更せずにUIView内の画像をスケーリングすることは可能ですか? (つまり、画像が UIView よりも大きく拡大しても、画像を UIView の境界にクリップします。)

別の SO 投稿で、UIView で画像をスケーリングするコードを見つけました。

view.transform = CGAffineTransformScale(CGAffineTransformIdentity, _scale, _scale);

ただし、これはビューの境界に影響を与えるようであり、それらを大きくするため、コンテンツが大きくなるにつれて、UIView の描画が他の近くの UIView を踏みにじるようになりました。クリッピング境界を同じに保ちながら、その内容を拡大することはできますか?

4

1 に答える 1

1

画像をスケーリングする最も簡単な方法は、contentMode プロパティを設定して UIImageView を使用することです。

画像を表示するために UIView を使用する必要がある場合は、UIView で画像を再描画してみてください。

1.UIViewのサブクラス

2. drawRect で画像を描画します

//the followed code draw the origin size of the image

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    [_yourImage drawAtPoint:CGPointMake(0,0)];
}

//if you want to draw as much as the size of the image, you should calculate the rect that the image draws into

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    [_yourImage drawInRect:_rectToDraw];
}

- (void)setYourImage:(UIImage *)yourImage
{
    _yourImage = yourImage;

    CGFloat imageWidth = yourImage.size.width;
    CGFloat imageHeight = yourImage.size.height;

    CGFloat scaleW = imageWidth / self.bounds.size.width;
    CGFloat scaleH = imageHeight / self.bounds.size.height;

    CGFloat max = scaleW > scaleH ? scaleW : scaleH;

    _rectToDraw = CGRectMake(0, 0, imageWidth * max, imageHeight * max);
}
于 2013-10-24T03:28:17.267 に答える