How to remove spacing from UIImageView aspect fit in Swift
Asked Answered
Q

3

14

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

Quip answered 10/9, 2015 at 23:8 Comment(0)
L
3

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

Lsd answered 17/9, 2015 at 1:58 Comment(3)
While the answer could be more elaborate it did gave me some hints to solve my problem, thanks !Adularia
@thibautnoah have you fixed that? Kindly help meTyr
@Tyr i think my answer below is more than enough mateAdularia
A
0

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
Adularia answered 20/12, 2016 at 17:26 Comment(0)
S
0

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()
    }
Scotfree answered 14/8, 2019 at 18:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.