CGImageCreateWithMask() 不返回蒙版图像

CGImageCreateWithMask() not returning a masked image

我正在尝试用另一个 UIImage 掩盖 UIImage。要遮罩的图像是从颜色渐变生成的,遮罩是 [UIImage systemImageNamed:]。问题是蒙版后得到的 UIImage 看起来像没有应用蒙版的渐变。我已经提供了创建渐变和用其他图像掩盖它的方法。任何人都可以确定这里出了什么问题吗?提前致谢!

// Creating the image

UIImage *mask = [UIImage systemImageNamed:@"person.crop.circle.fill.badge.plus"];
UIImage *gradient = [UIImage imageWithGradientColours:@[[UIColor redColor], [UIColor greenColor]] size:mask.size];
UIImage *masked = [gradient imageByApplyingMaskImage:mask];
UIImageView *imageView = [[UIImageView alloc] initWithImage:masked];
[self.view addSubview:imageView];

// UIImage+Gradient
// Generates a UIImage of the specified size with a gradient in the specified colours

+ (UIImage*)imageWithGradientColours:(NSArray*)colours size:(CGSize)size {
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, 0, size.width, size.height);
    NSMutableArray *cgColours = [NSMutableArray array];
    for (UIColor *colour in colours) {
        [cgColours addObject:(id)colour.CGColor];
    }
    gradientLayer.colors = cgColours;

    UIGraphicsBeginImageContext(size);
    [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return [gradientImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

// UIImage+MaskWithImage
// Returns a UIImage with a mask applied using the provided mask image
- (UIImage *)imageByApplyingMaskImage:(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(self.CGImage, mask);
    UIImage *maskedImage = [UIImage imageWithCGImage:maskedImageRef scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationUp];

    CGImageRelease(mask);
    CGImageRelease(maskedImageRef);

    // returns new image with mask applied
    return maskedImage;
}

啊,我找到答案了。在它作为掩码正常工作之前,我必须对掩码图像进行一些处理:

- (UIImage *)imageAsMask {
    //Ensure the image isn't rendered as template
    //Tint the image with [UIColor blackColor]
    UIImage *mask = [[self imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] imageWithTintColor:[UIColor blackColor]];

    //Context for redrawing the mask
    UIGraphicsBeginImageContextWithOptions(mask.size, YES, 1.0);
    //Fill the mask with white background
    [[UIColor whiteColor] setFill];
    CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, mask.size.width, mask.size.height));

    //Draw the black mask on top of the white background
    [mask drawAtPoint:CGPointZero];

    //Get the image from the context and return
    mask = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return mask;
}

然后为了生成蒙版图像,我只是这样做:

UIImage *mask = [[UIImage systemImageNamed:@"person.crop.circle.fill.badge.plus"] imageAsMask];
UIImage *gradient = [UIImage imageWithGradientColours:@[[UIColor redColor], [UIColor greenColor]] size:mask.size];
UIImage *masked = [gradient imageByApplyingMaskImage:mask];