我正在尝试使用 ios 中的蒙版创建具有自定义形状图案的个人资料图片图像。这是我用来创建蒙版图像的内容:
- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef maskedImageRef = CGImageCreateWithMask([image CGImage], mask);
UIImage *maskedImage = [UIImage imageWithCGImage:maskedImageRef];
CGImageRelease(mask);
CGImageRelease(maskedImageRef);
// returns new image with mask applied
return maskedImage;
}
实现实例后,我正在调用: *(_profileImage is global UIImage)
UIImage *maskImage = [UIImage imageNamed:@"maskProfilePicture.png"];
UIImage *maskedImage = [self maskImage:_profileImage withMask:maskImage];
和结果(应该在掩码的子视图中添加图像。但似乎掩码图像在需要掩码的图像上重叠):
蒙版图像:(可能是我在 Photoshop 中创建的蒙版图像属性错误 :)