我是iOS新手,我想知道我是否可以获得UIBezierPath
的{{1}}。我有一个UIImage
的面部布局,想要获得UIImage
,这有助于我裁剪UIBezierPath
。
或者,任何人都可以告诉我有关裁剪UIImage
的其他方法吗?但要确保裁剪处于自定义形状(如:面部,心脏等),而不是矩形。
答案 0 :(得分:4)
以下是用图像屏蔽图像的代码:
- (UIImage *)cerateImageFromImage:(UIImage *)image
withMaskImage:(UIImage *)mask {
CGImageRef imageRef = image.CGImage;
CGImageRef maskRef = mask.CGImage;
CGImageRef imageMask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef),
NULL,
YES);
CGImageRef maskedReference = CGImageCreateWithMask(imageRef, imageMask);
CGImageRelease(imageMask);
UIImage *maskedImage = [UIImage imageWithCGImage:maskedReference];
CGImageRelease(maskedReference);
return maskedImage;
}
<强>用法:强>
UIImage *image = [UIImage imageNamed:@"Photo.png"];
UIImage *mask = [UIImage imageNamed:@"Mask.png"];
self.imageView.image = [self cerateImageFromImage:image
withMaskImage:mask];