开发者

Know the real bounds of an image in UIImageView

开发者 https://www.devze.com 2022-12-21 06:46 出处:网络
I am usin开发者_运维技巧g an UIImageView to load an image with aspectfit. Hence the image does use the whole UIImageView surface. How to know what is the bounds of the image inside the UIImageView?

I am usin开发者_运维技巧g an UIImageView to load an image with aspectfit. Hence the image does use the whole UIImageView surface. How to know what is the bounds of the image inside the UIImageView?

Thanks in advance


CGFloat aspectRatioX = imageView.bounds.size.width/imageView.image.size.width;
CGFloat aspectRatioY = imageView.bounds.size.height/imageView.image.size.height;
if ( aspectRatioX < aspectRatioY )
    imageRect = CGRectMake(0, (imageView.bounds.size.height - aspectRatioX*imageView.image.size.height)*0.5f, imageView.bounds.size.width, aspectRatioX*imageView.image.size.height);
else
    imageRect = CGRectMake((imageView.bounds.size.width - aspectRatioY*imageView.image.size.width)*0.5f, 0, aspectRatioY*imageView.image.size.width, imageView.bounds.size.height);
0

精彩评论

暂无评论...
验证码 换一张
取 消