Walter M
Walter M

Reputation: 5533

How to remove spacing from UIImageView aspect fit in Swift

I created a UIImageView and assigned it an UIImage. I set the UIImageView content mode to Aspect Fit. How do I remove the padding from the top and bottom of the image, or how do I resize the UIImageView to wrap around the image? I tried everything.

If you are going to say to get the new size of the image, it won't work. Getting the image size inside the UIImageView only gives the width and height of the original image.

Please do not link other posts. I have read them already. They do not work.

enter image description here

Upvotes: 14

Views: 4952

Answers (3)

Almudhafar
Almudhafar

Reputation: 887

It's too easy when you are using NSLayoutConstraint

    let imageView = UIImageView()
    imageView.contentMode = .scaleAspectFit
    view.addSubview(imageView)
    imageView.translatesAutoresizingMaskIntoConstraints = false

    imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    imageView.widthAnchor.constraint(equalToConstant: 250).isActive = true
    let heightLayout = imageView.heightAnchor.constraint(equalToConstant: 250)
    heightLayout.isActive = true


    imageView.image = UIImage(named: "SomeImage")
    if let size = imageView.image?.size {
        heightLayout.constant = size.height / size.width * 250 // 250 is imageView width
        view.layoutIfNeeded()
    }

Upvotes: 0

thibaut noah
thibaut noah

Reputation: 1514

So, let's say you have a UIImageView set to equal width in relation to the view. One way to redraw your image to fit would be to use Core Graphics. What you do is calculate the scale factor you need to redraw the image to fit properly. You can find some great samples here :

https://github.com/natecook1000/Image-Resizing/blob/master/Image%20Resizing/ImageResizingMethods.swift

and a good tutorial with benchmarks to understand things better :

http://nshipster.com/image-resizing/

Sample of code i used (which is basically one of the github methods modify to suit my needs) :

            let image = UIImage(data: data!)
            let oldWidth = image!.size.width
            let scaleFactor = UIWindow().screen.bounds.width / oldWidth
            let cgImage = image!.CGImage

            let width = Double(CGImageGetWidth(cgImage)) * Double(scaleFactor)
            let height = Double(CGImageGetHeight(cgImage)) * Double(scaleFactor)
            let bitsPerComponent = CGImageGetBitsPerComponent(cgImage)
            let bytesPerRow = CGImageGetBytesPerRow(cgImage)
            let colorSpace = CGImageGetColorSpace(cgImage)
            let bitmapInfo = CGImageGetBitmapInfo(cgImage)

            let context = CGBitmapContextCreate(nil, Int(width), Int(height), bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo.rawValue)

            CGContextSetInterpolationQuality(context, .High)

            CGContextDrawImage(context, CGRect(origin: CGPointZero, size: CGSize(width: CGFloat(width), height: CGFloat(height))), cgImage)

            let scaledImage = CGBitmapContextCreateImage(context).flatMap { return UIImage(CGImage: $0) }
            imageView.image = scaledImage

Upvotes: 0

MailE
MailE

Reputation: 172

You may calculate it manually for UIImageView's frame sizes that Aspect Fit with your image, or use AVMakeRectWithAspectRatioInsideRect(method from AVFoundation).

Upvotes: 3

Related Questions