我正在尝试创建一个弹出我的xcode应用程序的框,只显示该大小的图像.我有以下内容
UIImageView *newView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"beach.jpg"]];
Run Code Online (Sandbox Code Playgroud)
但这会创建完整尺寸的图像并显示它.我怎么只在50以上和200下降时显示为100w 100h?我会用CGRect吗?
另外,我可以点击它使它消失吗?
编辑#1 我不想调整图像大小 - 宁愿只拉出100平方像素并将它们放在一个帧中.
Par*_*ram 76
请尝试使用以下代码.
MyImageview.contentMode = UIViewContentModeScaleAspectFill;
MyImageview.clipsToBounds = YES;
Run Code Online (Sandbox Code Playgroud)
eva*_*hin 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];
Run Code Online (Sandbox Code Playgroud)
Yuc*_*ong 13
您还可以通过选中故事板中的" 剪辑子视图"选项来实现裁剪效果.
或使用Swift 4+的代码中的等效设置
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23687 次 |
| 最近记录: |