【发布时间】:2016-04-09 00:41:35
【问题描述】:
有没有一种标准的方法可以根据给定的路径(CGPath)来剪辑一大块图像(UIImage)? (路径是椭圆、星形、多边形……) 我现在的问题与这个有关: AVCaptureStillImageOutput area selection 暂时没有答案,我仍然没有解决方案。
【问题讨论】:
标签: ios swift uiimage cgpath image-clipping
有没有一种标准的方法可以根据给定的路径(CGPath)来剪辑一大块图像(UIImage)? (路径是椭圆、星形、多边形……) 我现在的问题与这个有关: AVCaptureStillImageOutput area selection 暂时没有答案,我仍然没有解决方案。
【问题讨论】:
标签: ios swift uiimage cgpath image-clipping
使用此代码
CGPathRef path = CGPathCreateWithEllipseInRect(CGRectMake(50.0,50.0,300.0,200.0), nil);
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor blackColor].CGColor);
CGContextAddPath(UIGraphicsGetCurrentContext(), path);
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [UIColor redColor].CGColor);
CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathFillStroke);
UIImage *maskImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage *originalImage = [UIImage imageNamed:@"image"];
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextClipToMask(UIGraphicsGetCurrentContext(), CGRectMake(0.0,0.0,300.0,200.0), maskImage.CGImage);
[captureImage drawAtPoint:CGPointZero];
UIImage *clippedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
【讨论】: