- (UIImage *)maskImage:(UIImage *)originalimage topath:(UIBezIErPath *)path { UIGraphicsBeginImageContextWithOptions(originalimage.size,NO,0); [path addClip]; [originalimage drawAtPoint:CGPointZero]; UIImage *maskedImage = UIGraphicsGetimageFromCurrentimageContext(); UIGraphicsEndImageContext(); return maskedImage;}
如果要使用多个路径的并集,则更难,因为Quartz没有任何直接计算两个路径的并集的函数.一种方法是将每个路径逐个填入蒙版,然后通过掩码绘制图像:
- (UIImage *)maskedImage{ CGRect rect = CGRectZero; rect.size = self.originalimage.size; UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0); { [[UIcolor blackcolor] setFill]; UIRectFill(rect); [[UIcolor whitecolor] setFill]; for (UIBezIErPath *path in self.paths) [path fill]; } UIImage *mask = UIGraphicsGetimageFromCurrentimageContext(); UIGraphicsEndImageContext(); UIGraphicsBeginImageContextWithOptions(rect.size,0.0); { CGContextClipToMask(UIGraphicsGetCurrentContext(),rect,mask.CGImage); [self.originalimage drawAtPoint:CGPointZero]; } UIImage *maskedImage = UIGraphicsGetimageFromCurrentimageContext(); UIGraphicsEndImageContext(); return maskedImage;}总结
以上是内存溢出为你收集整理的iOS UIImage剪辑路径全部内容,希望文章能够帮你解决iOS UIImage剪辑路径所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)