Marshall
Marshall

Reputation: 151

How to remove empty space in an image when using Aspect Fit?

I display the data in a tableview
In Content Mode of image, I choose Aspect Fit
But I got something like this

with background.red
How to remove empty space around image?
I tried to find some answers, but unsuccessfully
Please help

When i use aspectFill

I use this code to show image from url
I use Kingfisher

 cell.image_of_new.kf.indicatorType = .activity
    cell.image_of_new.kf.setImage(
        with: url,
        placeholder: UIImage(named: "placeholderImage"),
        options: [
            .scaleFactor(UIScreen.main.scale),
            .transition(.fade(1)),
            .cacheOriginalImage
        ])
    {
        result in
        self.tableview.beginUpdates()
        self.tableview.endUpdates()
    }

Upvotes: 1

Views: 1347

Answers (1)

Duncan C
Duncan C

Reputation: 131418

Aspect fit is to designed to leave blank space if the image is a different shape than the frame into which you are drawing it.

The "aspect" part means it won't stretch the image to fit the target frame.

The "fit" part means it will be drawn so that every part of the source image fits into the destination frame. Nothing gets clipped.

Your target frame is tall and skinny (portrait) and your image is wide (landscape.) To draw it into the tall skinny frame without stretching or clipping, you will have blank space at the top and bottom.

By contrast, aspect fill will draw the image into the target rectangle without distorting the source image, but trim away parts of the source image.

Upvotes: 1

Related Questions