Fook Fook - 5 months ago 345
Swift Question

Resizing UIImageView frame after scaleAspectFit

I created a

UIImageView
in IB with a Content Mode of Aspect Fit. I'm then loading a large image to that view from a URL.

@IBOutlet weak var pictureView: UIImageView!

NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, _, error) -> Void in
guard let data = data where error == nil,
let image = UIImage(data: data)
else {
print(error?.localizedDescription)
return
}

pictureView.image = image
pictureView.clipsToBounds = true
setNeedsLayout()
layoutIfNeeded()

}).resume()


The image loads in the the
UIImageView
, and Aspect Fit resizes the image correctly to display it all in the frame, however the frame of the
UIImageView
is set to the size of the original large image. I have this huge frame wrapping this smaller resized image. I need the
UIImageView
frame to tightly wrap the Aspect Fit UIImage, throwing off all my other formatting.

Huge frame

Do I need to somehow calculate a new frame size manually for the
UIImageView
, or am I missing a setting somewhere?

EDIT:

I've also tried the following to resize the
UIImageView
frame around the image:

pictureView.frame = AVMakeRectWithAspectRatioInsideRect(picture.size, pictureView.frame)


and...

pictureView.frame = CGRectMake(self.pictureView.frame.origin.x, self.pictureView.frame.origin.y, pictureImage.size.width, pictureImage.size.height)


And I've also tried playing with the compression resistance settings.

Answer

Well there a few things that are involved.

The dependencies :

1) The image size. If the image height/width ratio is not greater than or equal to ImageView height/width ratio, then there will be black space shown, as there will be less ratio to match to the ImageView.

2) I am unsure of the constrains of your imageView, since I can see it is an @IBOutlet.

I am confused on your sentence "however the frame of the UIImageView is set to the size of the original large image. I have this huge frame wrapping this smaller resized image."

I believe what you are wanting to do is set the image into the imageView, but it seems that you are setting the UIImageView into the image size then the image into UIImageView, which makes it really confusing to debug. Could you explain more detailed exactly what you are wanting to happen?

EDIT

There is an ImageView with a frame -- this frame will NEVER be different.

The contentMode will change how the image is displayed on the imageView

The UIViewContentModeScaleAspectFit and UIViewContentModeScaleAspectFill modes scale the image to fit or fill the space while maintaining the image’s original aspect ratio.

The UIViewContentModeScaleToFill value scales the image without regard to the original aspect ratio, which can cause the image to appear distorted.

UIViewContentModeScaleToFill The option to scale the content to fit the size of itself by changing the aspect ratio of the content if necessary. *(Wouldn't recommend for most cases)

UIViewContentModeScaleAspectFit The option to scale the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent. *(This one guarantees that the WHOLE IMAGE fits on the screen. So if the image is smaller ratio than the screen, then there will be black on some edges, either top/bottom or left/right)

UIViewContentModeScaleAspectFill The option to scale the content to fill the size of the view. Some portion of the content may be clipped to fill the view’s bounds. *(This one guarantees that the WHOLE IMAGEVIEW is taken)

Comments