How to resize a UIImageView to fit the underlying image without moving it?

后端 未结 2 1478
没有蜡笔的小新
没有蜡笔的小新 2021-02-15 15:09

I have a UIImageView whose frame, set before the image is loaded, is always too large for the image, so when I try to round the corners, for example, nothing happens.

Ho

相关标签:
2条回答
  • 2021-02-15 16:12

    try something along the following lines.. not tested

    CGSize imageSize = image.Size;
    CGPoint oldCenter = imageView.center;
    
    // now do some calculations to calculate the new frame size
    CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height);
    imageView.frame = rect;
    imageView.center = oldCenter;
    
    0 讨论(0)
  • 2021-02-15 16:12

    If you change the bounds of a UIView the center will not change.

    CGRect bounds;
    
    bounds.origin = CGPointZero;
    bounds.size = newImage.size;
    
    imageView.bounds = bounds;
    imageView.image = newImage;
    
    0 讨论(0)
提交回复
热议问题