选择图像的一部分(剪裁)

Rob*_*bin 5 iphone cocoa-touch objective-c uiimageview uiimage

这可能不像我想象的那么难,但我怎样才能选择图像的一部分?请考虑以下示例:

alt text http://img683.imageshack.us/img683/2680/imagepart.jpg

灰色区域是PNG或JPG格式的图像,现在我想从中选择红色的80x80像素区域.应显示红色区域,其余不显示.我尝试了很多方法:

  • 使UIImageView的clipsToBounds为YES并尝试偏移图像(不起作用)
  • 剪辑上部视图(不起作用)

我也看了一下绘图功能,但我还没有使用它们,这对我来说似乎有点牵强.有什么明显的方法可以做我想做的事情,我错过了吗?我不希望图像被调整大小,只是剪裁.

谢谢你的时间!

wil*_*ood 7

这个问题中窃取下面的代码并查找HitScan的答案.

CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
// or use the UIImage wherever you like
[UIImageView setImage:[UIImage imageWithCGImage:imageRef]]; 
CGImageRelease(imageRef);
Run Code Online (Sandbox Code Playgroud)


Ale*_*yne 4

如果您只想显示图像的中心,请UIImageView通过 IB 将内容模式设置为“Aspect Fill”。这会将其居中并剪掉任何多余的东西。

或者在代码中

myImageView.contentMode = UIViewContentModeScaleAspectFill;
Run Code Online (Sandbox Code Playgroud)