ios 裁剪 UIImage 以适合框架图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14609132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Crop UIImage to fit a frame image
提问by CainaSouza
I need to crop a UIImage, taken with the device camera, so that it fits inside another UIImage, which represents a frame(with rounded borders and so on). Check the image below:
我需要裁剪 a UIImage,用设备相机拍摄,以便它适合另一个UIImage,它代表 a frame(带有圆形边框等)。检查下面的图像:
Using Aspect Fill
使用纵横填充
Using Aspect Fit
使用纵横匹配
So, what I need is to remove the image excess that is out of the frame bounds.
I tried using UIBezierPath, CGImageRefand other methods that I Googled but I'm not finding a solution for this.
所以,我需要的是去除超出帧边界的图像。我尝试使用UIBezierPath,CGImageRef以及我在 Google 上搜索过的其他方法,但我没有找到解决方案。
回答by Pier-Luc Gendreau
In Interface Builder, use the following configuration:
在 Interface Builder 中,使用以下配置:


There are two important settings, namely:
有两个重要的设置,即:
Mode: Aspect FillClip Subviews
Mode: Aspect FillClip 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.
这将正确地用图像填充视图,保持其纵横比并隐藏不适合的内容。
回答by Remizorrr
make a IBOutletin your controller.
IBOutlet在您的控制器中创建一个。
@property (retain)IBOutlet UIImageView* imageView;
and in -(void) viewDidLoadset
并在-(void) viewDidLoad集合
imageView.layer.masksToBounds = YES;
回答by sergio
In interface Builder, access the Modemenu inside of the detail pane (the fourth one) and choose the right one for your UIImageView(I guess "center" or "aspect fit").
在界面生成器中,访问详细信息窗格(第四个)内的“模式”菜单并为您选择正确的菜单UIImageView(我猜是“中心”或“方面适合”)。


OLD ANSWER:
旧答案:
You can use the contentGravityproperty of CALayer to make it work
您可以使用contentGravityCALayer的属性使其工作
A constant that specifies how the layer's contents are positioned or scaled within its bounds.
@property(copy) NSString *contentsGravity
一个常量,指定层的内容如何在其边界内定位或缩放。
@property(copy) NSString *contentsGravity
回答by Ismael
Use an UIImageViewand do
使用UIImageView和做
imageView.contentMode = UIViewContentModeScaleAspectFit;

