我有一个UIImage,想要削减它。我有4个CGPoints的边缘。我尝试用面具做,但它只是将颜色改为透明。我需要一个全新的UIImage。有人能帮我吗? (在Objective-C中)
谢谢!
答案 0 :(得分:0)
使用这样的代码。 originalImage
是图片,p0
,p1
,p2
和p3
是CGPoint
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();
我没有测试代码。因此,翻译和裁剪可能没有按正确的顺序设置或使用错误的符号。