创建一个裁剪图像的UIImageView,在50x和200y时只显示100w 100h

时间:2012-12-05 02:41:09

标签: ios uiimageview

我正在尝试创建一个弹出我的xcode应用程序的框,只显示该大小的图像。我有以下

UIImageView *newView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"beach.jpg"]];

但这会创建完整尺寸的图像并显示它。我怎么只在50以上和200下降时显示为100w 100h?我会使用CGRect吗?

另外,我可以点击它使它消失吗?

编辑#1 我不想调整图像的大小 - 宁愿只拉出100平方像素并将它们放在一个帧中。

3 个答案:

答案 0 :(得分:76)

请尝试使用以下代码。

MyImageview.contentMode = UIViewContentModeScaleAspectFill; 
MyImageview.clipsToBounds = YES;

答案 1 :(得分:15)

您可以使用以下代码裁剪图片:

CGRect cropRegion = CGRectMake(50, 200, 100, 100);
UIImage *image = [UIImage imageNamed:@"beach.jpg"];
CGImageRef subImage = CGImageCreateWithImageInRect(image.CGImage, cropRegion);
UIImage *croppedImage = [UIImage imageWithCGImage:subImage];
UIImageView *newView = [[UIImageView alloc] initWithImage:croppedImage];

答案 2 :(得分:13)

您还可以通过查看情节提要中的剪辑子视图选项来实现裁剪效果。

enter image description here

使用 Swift 4 +

的代码中的等效设置
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true