Ios UIBezierPath剪辑UIIimage
我有一个UIBezierPath,我想从源图像中得到一个PNG图像,它的形状像pathIos UIBezierPath剪辑UIIimage,ios,objective-c,uibezierpath,Ios,Objective C,Uibezierpath,我有一个UIBezierPath,我想从源图像中得到一个PNG图像,它的形状像path UIGraphicsBeginImageContextWithOptions(sourceImage.size, NO, 0); [bpath addClip]; [sourceImage drawAtPoint:CGPointZero]; UIImage *maskedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsBeginImageContextWithOptions(sourceImage.size, NO, 0);
[bpath addClip];
[sourceImage drawAtPoint:CGPointZero];
UIImage *maskedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
但是面具是空的。当我使用路径测试填充时,我的路径似乎有效。请告知我如何获得形状类似UIBezierPath的图像部分您的代码很好,问题是您的剪裁遮罩或图像。我刚刚尝试了下面的方法,效果非常完美:
UIImage *sourceImage = [UIImage imageNamed:@"example"];
UIBezierPath *bpath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, sourceImage.size.width, sourceImage.size.height) cornerRadius:50];
UIGraphicsBeginImageContextWithOptions(sourceImage.size, NO, 0);
[bpath addClip];
[sourceImage drawAtPoint:CGPointZero];
UIImage *maskedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
前两行是我的;最后五个是你的。结果看起来和我预期的完全一样,所以要么加载图像时出现问题,要么在绘制和使用Quick Look检查结果时放置断点,要么你的遮罩不是你想象的那样
有趣的事实:UIBezierPath还与Quick Look兼容,因此您应该能够在调试器中检查bpath的内容,以确保它是您认为的内容。考虑到图像的大小,请确保路径大小合理 我明白了。我写了我的解决方案 关键是将图像绘制为
UIGraphicsPushContext(context);
UIBezierPath *bpath = [UIBezierPath bezierPath];
// Add lines to path
[bpath closePath];
[bpath addClip];
[_overlayImage drawInRect:theRect blendMode:kCGBlendModeMultiply alpha:0.4];
UIGraphicsPopContext();