沿着UIBezierPath将UIImage分成两部分

And*_*y05 2 objective-c uiimage ios uibezierpath

如何将UIImage黑线划分为两部分.上轮廓集UIBezierPath.

我需要得到两个结果UIImage.那有可能吗?

Don*_*mer 7

以下一组例程创建UIImage的版本,其中只包含路径的内容,或者只包含该路径之外的内容.

两者都使用了compositeImage使用CGBlendMode 的方法.CGBlendMode非常强大,可以屏蔽任何你可以绘制的东西.调用compositeImage:使用其他混合模式可以有趣(如果不是总是有用的)效果.有关所有模式,请参阅CGContext参考.

我在对你的OP的评论中描述的剪辑方法确实有效,并且可能更快,但只有你有UIBezierPaths定义你想要剪辑的所有区域.

- (UIImage*) compositeImage:(UIImage*) sourceImage onPath:(UIBezierPath*) path usingBlendMode:(CGBlendMode) blend;
{
    // Create a new image of the same size as the source.
    UIGraphicsBeginImageContext([sourceImage size]);

    // First draw an opaque path...
    [path fill];
    // ...then composite with the image.
    [sourceImage drawAtPoint:CGPointZero blendMode:blend alpha:1.0];

    // With drawing complete, store the composited image for later use.
    UIImage *maskedImage = UIGraphicsGetImageFromCurrentImageContext();

    // Graphics contexts must be ended manually.
    UIGraphicsEndImageContext();

    return maskedImage;
}

- (UIImage*) maskImage:(UIImage*) sourceImage toAreaInsidePath:(UIBezierPath*) maskPath;
{
    return [self compositeImage:sourceImage onPath:maskPath usingBlendMode:kCGBlendModeSourceIn];
}

- (UIImage*) maskImage:(UIImage*) sourceImage toAreaOutsidePath:(UIBezierPath*) maskPath;
{
    return [self compositeImage:sourceImage onPath:maskPath usingBlendMode:kCGBlendModeSourceOut];
}
Run Code Online (Sandbox Code Playgroud)