使用4个CGPoints剪切图像

时间:2016-08-12 11:59:00

标签: objective-c uiimage

我有一个UIImage,想要削减它。我有4个CGPoints的边缘。我尝试用面具做,但它只是将颜色改为透明。我需要一个全新的UIImage。有人能帮我吗? (在Objective-C中)

谢谢!

1 个答案:

答案 0 :(得分:0)

使用这样的代码。 originalImage是图片,p0p1p2p3CGPoint s作为输入。 clippedImage是生成的图片:

// Create clipping path
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:p0];
[path addLineToPoint:p1];
[path addLineToPoint:p2];
[path addLineToPoint:p3];
[path closePath];

// Get boundary rectangle
CGRect rect = path.bounds;

// Create graphis context with translation and clipping
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 1);
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -rect.origin.x, -rect.origin.y)
[path addClip];

// draw image
[originalImage drawAtPoint:CGPointZero];

// create resulting image
UIImage *clippedImage = UIGraphicsGetImageFromCurrentImageContext();

// clean up
UIGraphicsEndImageContext();

我没有测试代码。因此,翻译和裁剪可能没有按正确的顺序设置或使用错误的符号。