ios 创建一个裁剪图像并在 50x 和 200y 下仅显示 100w 100h 的 UIImageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13715456/
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
create a UIImageView that crops an image and displays only 100w 100h at 50x and 200y
提问by timpone
I am trying to create a box that pops up in my xcode app that just displays an image of that size. I have the following
我正在尝试创建一个在我的 xcode 应用程序中弹出的框,该框仅显示该大小的图像。我有以下
UIImageView *newView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"beach.jpg"]];
but this creates the full size image and just shows it. How would I only show it as 100w by 100h at 50 over and 200 down? Would I use CGRect?
但这会创建全尺寸图像并仅显示它。我如何仅将其显示为 100w x 100h 在 50 上和 200 下?我会使用 CGRect 吗?
Also, can I make it so that clicking it causes it to disappear?
另外,我可以让它点击它使其消失吗?
edit #1I don't want to resize down the image - would rather just pull the 100 squared pixels and place them in a frame.
编辑 #1我不想缩小图像的大小 - 宁愿只是拉出 100 个平方像素并将它们放在一个框架中。
回答by Paramasivan Samuttiram
Please try using the following code.
请尝试使用以下代码。
MyImageview.contentMode = UIViewContentModeScaleAspectFill;
MyImageview.clipsToBounds = YES;
回答by evanchin
You can crop image with below code:
您可以使用以下代码裁剪图像:
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];