如何用黑线把这个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/