CGContextClipToMask returning blank image

前端 未结 1 561
无人及你
无人及你 2021-02-14 07:54

I\'m new to Quartz. I have 2 images, a background, and a mask with cutout shape that I want to lay over the background in order to cut out a section. The resulting image should

相关标签:
1条回答
  • 2021-02-14 08:33

    After you clip context with mask you should actually draw something on that context.

    Besides that:

    • you should use UIGraphicsBeginImageContextWithOptions to support scale factor.
    • you're creating two context, one with UIGraphicsBeginImageContext and the other with CGBitmapContextCreate. One is enough :)
    • you should "flip" alpha of your mask, because according to docs The source samples of mask determine which points of the clipping area are changed. The other words transparent will become transparent

    Simple example code, where I clip image inside imageView and then set it back:

    UIGraphicsBeginImageContextWithOptions(self.imageView.frame.size, NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextTranslateCTM(context, 0.0, self.imageView.frame.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    CGImageRef maskImage = [[UIImage imageNamed:@"mask.png"] CGImage];
    CGContextClipToMask(context, self.imageView.bounds, maskImage); 
    
    CGContextTranslateCTM(context, 0.0, self.imageView.frame.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    [[self.imageView image] drawInRect:self.imageView.bounds];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    self.imageView.image = image;
    

    Example using CGImageCreateWithMask:

    UIImage *image = [UIImage imageNamed:@"image"];
    
    CGImageRef originalMask = [UIImage imageNamed:@"mask"].CGImage;
    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(originalMask),
                                        CGImageGetHeight(originalMask),
                                        CGImageGetBitsPerComponent(originalMask),
                                        CGImageGetBitsPerPixel(originalMask),
                                        CGImageGetBytesPerRow(originalMask),
                                        CGImageGetDataProvider(originalMask), nil, YES);
    
    CGImageRef maskedImageRef = CGImageCreateWithMask(image.CGImage, mask);
    
    UIImage *maskedImage = [UIImage imageWithCGImage:maskedImageRef scale:image.scale orientation:image.imageOrientation];
    
    CGImageRelease(mask);
    CGImageRelease(maskedImageRef);
    
    0 讨论(0)
提交回复
热议问题