gpt4 book ai didi

ios - 在 UIImageView 中移动 UIImage

转载 作者:可可西里 更新时间:2023-11-01 05:40:47 26 4
gpt4 key购买 nike

我有一个 UIImageView(红色方 block ),它将显示一个必须缩放的 UIImage(我可以接收比 UIImageView )。缩放后,UIImage 的显示部分是它的中心。

我需要的是显示图片中蓝色方 block 的部分,如何存档?

我只能获取图像尺寸(高度和宽度),但它显示原始尺寸,而它应该是缩放后的尺寸。

self.viewIm = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 120, 80)];
self.viewIm.backgroundColor = [UIColor greenColor];
self.viewIm.layer.borderColor = [UIColor redColor].CGColor;
self.viewIm.layer.borderWidth = 5.0;
UIImage *im = [UIImage imageNamed:@"benjen"];
self.viewIm.image = im;
self.viewIm.contentMode = UIViewContentModeScaleAspectFill;
// self.viewim.clipsToBounds = YES;
[self.view addSubview:self.viewIm];

enter image description here

最佳答案

要完成您想要做的事情,我建议查看 CALayercontentsRect属性(property)。

自从看到您的回答后,我一直在尝试找出正确的解决方案一段时间,但数学让我逃脱了,因为 contentsRect: 的 x 和 y 参数看起来有点神秘...但这里有一些代码可能会为您指明正确的方向......

float imageAspect = self.imageView.image.size.width/self.imageView.image.size.height;
float imageViewAspect = self.imageView.frame.size.width/self.imageView.frame.size.height;

if (imageAspect > imageViewAspect) {
float scaledImageWidth = self.imageView.frame.size.height * imageAspect;
float offsetWidth = -((scaledImageWidth-self.imageView.frame.size.width)/2);
self.imageView.layer.contentsRect = CGRectMake(offsetWidth/self.imageView.frame.size.width, 0.0, 1.0, 1.0);
} else if (imageAspect < imageViewAspect) {
float scaledImageHeight = self.imageView.frame.size.width * imageAspect;
float offsetHeight = ((scaledImageHeight-self.imageView.frame.size.height)/2);
self.imageView.layer.contentsRect = CGRectMake(0.0, offsetHeight/self.imageView.frame.size.height, 1.0, 1.0);
}

关于ios - 在 UIImageView 中移动 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28705697/

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