gpt4 book ai didi

ios - 如何在 UIImageView 中居中缩放(旋转)UIImage

转载 作者:行者123 更新时间:2023-11-28 22:38:22 24 4
gpt4 key购买 nike

我需要仅使用 CGAffineTransform 将 UIImage 居中并缩放到具有 contentMode UIViewContentModeTopLeft 的 UIImageView 中。我不能使用 contentMode UIViewContentModeCenter,因为我需要完整的 CGAffineTransform 信息。

到目前为止,我使用的是类似这样的东西,其中 _imageView 是一个 UIImageView,image 是一个 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 的情况下做到这一点吗?

最佳答案

试试这个:

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

关于ios - 如何在 UIImageView 中居中缩放(旋转)UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15280820/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com