UIImageView, setClipsToBounds and how my images are losing their head

后端 未结 2 1148
我在风中等你
我在风中等你 2021-01-02 03:58

I\'m developing an iOS 4 application.

I\'m using this code on an UIImageView on an UITableViewCell:

cell.photo.contentMode          


        
相关标签:
2条回答
  • 2021-01-02 04:54

    Afaik, the easiest way is to calculate the imageView's frame manually from the size of the image and the frame you want it to fit in.

    Create a view with the frame you're currently specifying for your imageView, make the imageView its subview. Set view.clipsToBounds = YES.

    Then when you assign an image, do something like

    CGFloat scaleX = view.bounds.size.width / image.size.width;
    CGFloat scaleY = view.bounds.size.height / image.size.height;
    CGFloat scale = MAX(scaleX, scaleY);
    imageView.frame = CGRectMake(0, 0, image.size.width * scale, image.size.height * scale);
    imageView.image = image;
    
    0 讨论(0)
  • 2021-01-02 05:01

    There is no built-in way to do this, but you can do it manually:

    UIGraphicsBeginImageContext(CGSizeMake(desiredWidth, desiredHeight));
    
    [originalImage drawAtPoint...];
    ...
    
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    0 讨论(0)
提交回复
热议问题