For a UIImageView
, I am using the aspect fill content mode. It's OK, but for some images, it will cut from the top because I used clipsToBounds = true
. So here is what I want: I want to make the two filters active at the same time, for example:
Here is an image view that I set to aspect fill:
...and an image view I set using contentMode = .top
:
So I want to merge these two content modes. Is it possible? Thanks in advance.
The Fit setting will resize an image to fit within the bounds of its container with no cropping. The Fill setting will expand an image to fill the whole container, which may result in some cropping depending on the dimensions of your container and the image you send into it.
👉 In the code, click on the top VStack . In the Attributes inspector, set the Alignment to leading. 👉 Click on the new bottom image in the preview (in the top preview of the news cell, not the bottom preview of the list). 👉 In the Attributes inspector, set the Aspect Ratio to Fill and the Resizing Mode to Stretch .
An object that manages image data in your app.
Swift. You can use the following options to position your image: scaleToFill. scaleAspectFit // contents scaled to fit with fixed aspect.
Update: device scaling is now properly handled, thanks to budidino for that!
You should resize the image, so that it will have the width of your image view, but by keeping its aspect ratio. After that, set the image view's content mode to .top
and enable clipping to bounds for it.
The resizeTopAlignedToFill
function is a modified version of this answer.
func setImageView() {
imageView.contentMode = .top
imageView.clipsToBounds = true
let image = <custom image>
imageView.image = image.resizeTopAlignedToFill(newWidth: imageView.frame.width)
}
extension UIImage {
func resizeTopAlignedToFill(newWidth: CGFloat) -> UIImage? {
let newHeight = size.height * newWidth / size.width
let newSize = CGSize(width: newWidth, height: newHeight)
UIGraphicsBeginImageContextWithOptions(newSize, false, UIScreen.main.scale)
draw(in: CGRect(origin: .zero, size: newSize))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}
Try this:
imageView.contentMode = UIViewContentModeTop;
imageView.image = [UIImage imageWithCGImage:image.CGImage scale:image.size.width / imageView.frame.size.width orientation:UIImageOrientationUp];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With