UIImageView.image bigger than UIImageView

偶尔善良 提交于 2019-12-06 03:11:47

I've faced the same problem, and tried a lot ways to fix it, finally, I did it. When you try to assign the image to your UIImageView, please resize your image as a thumbnail image, and use this thumbnail image to display. The following code just for your reference.

static func resizeImage(image:UIImage, toTheSize size:CGSize) -> UIImage{
    let scale = CGFloat(max(size.width/image.size.width,
    size.height/image.size.height))
    let width:CGFloat  = image.size.width * scale
    let height:CGFloat = image.size.height * scale;

    let rr:CGRect = CGRectMake( 0, 0, width, height);

    UIGraphicsBeginImageContextWithOptions(size, false, 0);
    image.drawInRect(rr)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext();
    return newImage
}

You most likely want the content mode to be "Aspect Fit" not "Aspect Fill".

cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

I think that it would be a clip subviews issue.

Check your UIImageView property.

  • Storyboard : check clip subviews in Attributes inspector
  • Code : imgView.clipsToBounds = YES
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!