Crop UIImage to fit a frame image

后端 未结 4 1201
眼角桃花
眼角桃花 2020-12-09 01:46

I need to crop a UIImage, taken with the device camera, so that it fits inside another UIImage, which represents a frame (with rounded

相关标签:
4条回答
  • 2020-12-09 02:11

    In interface Builder, access the Mode menu inside of the detail pane (the fourth one) and choose the right one for your UIImageView (I guess "center" or "aspect fit").

    enter image description here

    OLD ANSWER:

    You can use the contentGravity property of CALayer to make it work

    A constant that specifies how the layer's contents are positioned or scaled within its bounds.

           @property(copy) NSString *contentsGravity
    
    0 讨论(0)
  • 2020-12-09 02:14

    In Interface Builder, use the following configuration:

    enter image description here

    There are two important settings, namely:

    1. Mode: Aspect Fill

    2. Clip Subviews

    It can also be done programmatically:

    [imageView setContentMode:UIViewContentModeScaleAspectFill];
    [imageView setClipsToBounds:YES];
    

    This will correctly fill the view with the image, keep its aspect ratio and hide what doesn't fit.

    0 讨论(0)
  • 2020-12-09 02:16

    Use an UIImageView and do

    imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    0 讨论(0)
  • 2020-12-09 02:23

    make a IBOutlet in your controller.

    @property (retain)IBOutlet UIImageView* imageView;
    

    and in -(void) viewDidLoad set

    imageView.layer.masksToBounds = YES;
    
    0 讨论(0)
提交回复
热议问题