UIImageView.image bigger than UIImageView

人走茶凉 提交于 2019-12-07 13:58:01

问题


I have a UIImageView in a custom tableview cell. When I set the UIimageview property, the uiimageview.image is bigger than the uiimageview. Why? Here is the code:

cell.imageView.image=[UIImage imageNamed:@"BlueMooseLogo.png"];

And here is a link to what the cell looks like. The blue square is the background of the imageview, and the blue moose is the image. https://docs.google.com/document/d/1G7BYCriYbSGYHryP5OI7XHuVpqRKiNcBrxdY14wDgoA/edit?usp=sharing

If it's relevant, within interface builder, after I select the uiimageview, I have the view set to scale to fill.


回答1:


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
}



回答2:


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

cell.imageView.contentMode = UIViewContentModeScaleAspectFit;



回答3:


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


来源:https://stackoverflow.com/questions/29403128/uiimageview-image-bigger-than-uiimageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!