I have a UIImage and want to cut it. I have 4 CGPoints for the edges. I tried to do it with a mask, but it only change the color to transparent. I need a complete new UIImage. Can someone help me? (in Objective-C)
Thanks!
I have a UIImage and want to cut it. I have 4 CGPoints for the edges. I tried to do it with a mask, but it only change the color to transparent. I need a complete new UIImage. Can someone help me? (in Objective-C)
Thanks!
Use some code like this. originalImage
is the image and p0
, p1
, p2
and p3
are the CGPoint
s as input. clippedImage
is the resulting image:
// 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();
I didn't test the code. So it could be that the translation and the clipping aren't setup in the right order or use the wrong sign.