美文网首页
UIView 通过 UIBezierPath 来进行切图的简单实

UIView 通过 UIBezierPath 来进行切图的简单实

作者: CoderHG | 来源:发表于2017-09-07 23:50 被阅读65次

主题,实际上更专业的说法是抠图。
具体的逻辑是通过 UIBezierPath 可以描绘出不规则的形状,在通过这样不规则的形状进行抠图。
代码胜千言:


/**
 通过曲线返回视图对应的UIImage

 @param bezierPath 贝尔曲线
 @return 返回UIImage
 */
- (UIImage*)maskWithBezierPath:(UIBezierPath*)bezierPath {
    // 通过曲线创建一个图层
    CAShapeLayer* layer = [[CAShapeLayer alloc] init];
    layer.frame = self.bounds;
    layer.path = bezierPath.CGPath;
    layer.fillColor = [UIColor blackColor].CGColor;
    
    // 当前视图也变了
    self.layer.mask = layer;
    
    // 有UIView ---> UIImage
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    // 返回
    return image;
}

上面的实现很粗超,有很多的地方是没有考虑到的.比如起始点

谢谢~

相关文章

网友评论

      本文标题:UIView 通过 UIBezierPath 来进行切图的简单实

      本文链接:https://www.haomeiwen.com/subject/tqwwjxtx.html