UIImageView, setClipsToBounds and how my images are losing their head

纵饮孤独 提交于 2019-11-30 12:00:49

Afaik, the easiest way is to calculate the imageView's frame manually from the size of the image and the frame you want it to fit in.

Create a view with the frame you're currently specifying for your imageView, make the imageView its subview. Set view.clipsToBounds = YES.

Then when you assign an image, do something like

CGFloat scaleX = view.bounds.size.width / image.size.width;
CGFloat scaleY = view.bounds.size.height / image.size.height;
CGFloat scale = MAX(scaleX, scaleY);
imageView.frame = CGRectMake(0, 0, image.size.width * scale, image.size.height * scale);
imageView.image = image;

There is no built-in way to do this, but you can do it manually:

UIGraphicsBeginImageContext(CGSizeMake(desiredWidth, desiredHeight));

[originalImage drawAtPoint...];
...

UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!