0

我需要仅使用 CGAffineTransform 在 UIImageView 中使用 contentMode UIViewContentModeTopLeft 居中和缩放 UIImage。我不能使用 contentMode UIViewContentModeCenter 因为我需要完整的 CGAffineTransform 信息。

到目前为止,我正在使用这样的东西,其中 _imageView 是 UIImageView 并且图像是 UIImage。

_imageView.image = image;
CGFloat scale = fmaxf(_imageView.bounds.size.width / _imageView.image.size.width, _imageView.bounds.size.height / _imageView.image.size.height);
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, _imageView.image.size.width * 0.5, _imageView.image.size.height * 0.5);
transform = CGAffineTransformScale(transform, scale, scale);    
_imageView.transform = transform;

缩放是好的,但图像总是在右下角有一些偏移。有人知道如何在不使用 contentMode UIViewContentModeCenter 的情况下做到这一点吗?

4

1 回答 1

2

试试这个:

CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
scaleAnim.removedOnCompletion = YES;
[_imageView.layer addAnimation:scaleAnim forKey:nil];
于 2013-03-07T20:45:31.713 回答