objective-c - 沿着 UIBezierPath 将 UIImage 分成两部分

标签 objective-c ios uiimage uibezierpath

如何用黑线把这个UIImage分成两部分。 UIBezierPath 的上轮廓集。

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

最佳答案

下面的一组例程创建 UIImage 的版本,或者只有路径内部的内容,或者只有路径外部的内容。

两者都使用 compositeImage 方法,该方法使用 CGBlendMode。 CGBlendMode 非常强大,可以将您可以绘制的任何内容与您可以绘制的任何其他内容进行屏蔽。调用 compositeImage: 与其他混合模式可以产生有趣的(如果不是总是有用的)效果。查看CGContext Reference对于所有模式。

我在对您的 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];
}

关于objective-c - 沿着 UIBezierPath 将 UIImage 分成两部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10088629/

相关文章:

ios - 如何在运行时 "cleanly"期间更改 UIBarButtonItem 文本(编辑 --> 完成 --> 编辑)?

ios - 横屏模式下的 Swift SpriteKit iAd

ios - UIImage方法 "drawInRect"的内存泄漏

ios - 从字符串变量中获取的不仅仅是 char

objective-c - 具有自定义 UI 的 UIActivityViewController

ios - swrrevealviewcontrtoller 在侧面菜单打开/关闭时更改切换按钮图像?

iPhone - CGImageCreateWithImageInRect 旋转一些相机胶卷图片

iphone - FMDB SQLite 包装器和用户定义/自定义函数

ios - 在 iOS 的 tableview 中动态搜索

ios - 如何使用从 AVCaptureDevice 捕获的 NSData 和 NSDictionary 组合一个面向 "right"的 UIImage?