iOS UIImage剪辑路径

iOS UIImage剪辑路径,第1张

概述我正在使用用户使用UIBezierPath选择其中一部分的图像.如何删除/清除/清除不是该选择的一部分的所有内容? 有一条路很容易.只需将路径设置为剪切路径: - (UIImage *)maskImage:(UIImage *)originalImage toPath:(UIBezierPath *)path { UIGraphicsBeginImageContextWithOptions 我正在使用用户使用UIBezIErPath选择其中一部分的图像.如何删除/清除/清除不是该选择的一部分的所有内容?解决方法 有一条路很容易.只需将路径设置为剪切路径:
- (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剪辑路径所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1097193.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存