macos 使用 CICrop 裁剪 CIImage 无法正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9601242/
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
Cropping CIImage with CICrop isn't working properly
提问by Justin Boo
I'm having troubles cropping image. For me CICrop filter is not working properly. If my CIVector x and y (origins) are 0 everything working fine (image is cropped from left bottom corner), image is cropped by my rectangle width and height, but if CIVector origins (x and y) aren't 0 in my cropped image becomes space (because CICrop filter cropping from bottom left corner no matter what origins (x and y) are).
我在裁剪图像时遇到了麻烦。对我来说,CICrop 过滤器工作不正常。如果我的 CIVector x 和 y(原点)为 0 一切正常(图像从左下角裁剪),图像将按我的矩形宽度和高度裁剪,但如果 CIVector 原点(x 和 y)在我的裁剪中不为 0图像变成空间(因为 CICrop 过滤器从左下角裁剪,无论原点(x 和 y)是什么)。
I'm cropping CIImage with rectangle, source:
我正在用矩形裁剪 CIImage,来源:
CIVector *cropRect =[CIVector vectorWithX:150 Y:150 Z: 300 W: 300];
CIFilter *cropFilter = [CIFilter filterWithName:@"CICrop"];
[cropFilter setValue:myCIImage forKey:@"inputImage"];
[cropFilter setValue:cropRect forKey:@"inputRectangle"];
CIImage *croppedImage = [cropFilter valueForKey:@"outputImage"];
Output Image with CIVector X 150 and Y 150:(I drawn the border for clarity)
使用 CIVector X 150 和 Y 150 输出图像:(为了清晰起见,我画了边框)
Output Image with CIVector X 0 and Y 0:
使用 CIVector X 0 和 Y 0 输出图像:
Original Image:
原图:
What I'm doing wrong? Or is it supposed to do this?
我做错了什么?或者它应该这样做吗?
回答by Rob Keniger
Are you sure the output image is the size you are expecting? How are you drawing the output image?
您确定输出图像是您期望的尺寸吗?你如何绘制输出图像?
The CICrop
filter does not reduce the size of the original image, it just blanks out the content you don't want.
该CICrop
过滤器不降低原有图像的大小,它只是清空了你不想要的内容。
To get the result you want you probably need to just do this:
要获得您想要的结果,您可能只需要这样做:
[image drawAtPoint:NSZeroPoint fromRect:NSMakeRect(150, 150, 300, 300) operation:NSCompositeSourceOver fraction:1.0];
If you want an actual CIImage
as output rather than just drawing it, just do this:
如果你想要一个实际的CIImage
输出而不是仅仅绘制它,只需执行以下操作:
CIImage* croppedImage = [image imageByCroppingToRect:CGRectMake(150, 150, 300, 300)];
//you also need to translate the origin
CIFilter* transform = [CIFilter filterWithName:@"CIAffineTransform"];
NSAffineTransform* affineTransform = [NSAffineTransform transform];
[affineTransform translateXBy:-150.0 yBy:-150.0];
[transform setValue:affineTransform forKey:@"inputTransform"];
[transform setValue:croppedImage forKey:@"inputImage"];
CIImage* transformedImage = [transform valueForKey:@"outputImage"];
回答by Chris Vander Mey
It's important to note that the coordinate system of a view is top-left-corner, whereas CIImage is bottom left. This will make you crazy if you don't catch it when you're doing these transforms! This other post describes a one-directional conversion: Changing CGrect value to user coordinate system.
需要注意的是,视图的坐标系是左上角,而 CIImage 是左下角。如果你在做这些变换时没有抓住它,这会让你发疯!另一篇文章描述了单向转换:将 CGrect 值更改为用户坐标系。
回答by samwize
This is how CICrop
works -- it crop the rect you specified, and the un-cropped area becomes transparent. If you print extent
you will see that it is still the same original rect.
这就是CICrop
工作原理 - 它裁剪您指定的矩形,并且未裁剪的区域变得透明。如果您打印,extent
您会看到它仍然是相同的原始矩形。
As suggested, you can do a translation. This is now just 1 line, in Swift 5:
按照建议,您可以进行翻译。这现在只有 1 行,在 Swift 5 中:
let newImage = myCIImage.transformed(by: CGAffineTransform(translationX: -150, y: -150)