Creating mask with CGImageMaskCreate is all black (iphone)

后端 未结 3 961
猫巷女王i
猫巷女王i 2020-12-23 08:51

I\'m trying to create an image mask that from a composite of two existing images.

First I start with creating the composite which consists of a small image that is t

相关标签:
3条回答
  • 2020-12-23 09:12

    I tried the same thing with CGImageCreateWithMask, and got the same result. The solution I found was to use CGContextClipToMask instead:

    CGContextRef mainViewContentContext;
    CGColorSpaceRef colorSpace;
    
    colorSpace = CGColorSpaceCreateDeviceRGB();
    
    // create a bitmap graphics context the size of the image
    mainViewContentContext = CGBitmapContextCreate (NULL, targetSize.width, targetSize.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
    
    // free the rgb colorspace
    CGColorSpaceRelease(colorSpace);    
    
    if (mainViewContentContext==NULL)
        return NULL;
    
    CGImageRef maskImage = [[UIImage imageNamed:@"mask.png"] CGImage];
    CGContextClipToMask(mainViewContentContext, CGRectMake(0, 0, targetSize.width, targetSize.height), maskImage);
    CGContextDrawImage(mainViewContentContext, CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledWidth, scaledHeight), self.CGImage);
    
    
    // Create CGImageRef of the main view bitmap content, and then
    // release that bitmap context
    CGImageRef mainViewContentBitmapContext = CGBitmapContextCreateImage(mainViewContentContext);
    CGContextRelease(mainViewContentContext);
    
    // convert the finished resized image to a UIImage 
    UIImage *theImage = [UIImage imageWithCGImage:mainViewContentBitmapContext];
    // image is retained by the property setting above, so we can 
    // release the original
    CGImageRelease(mainViewContentBitmapContext);
    
    // return the image
    return theImage;
    
    0 讨论(0)
  • 2020-12-23 09:18
    - (UIImage*) maskImage:(UIImage *)image {
    
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
        UIImage *maskImage = [UIImage imageNamed:@"MaskFinal.png"];
        CGImageRef maskImageRef = [maskImage CGImage];
    
        // create a bitmap graphics context the size of the image
        CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
    
    
        if (mainViewContentContext==NULL)
            return NULL;
    
        CGFloat ratio = 0;
    
        ratio = maskImage.size.width/ image.size.width;
    
        if(ratio * image.size.height < maskImage.size.height) {
            ratio = maskImage.size.height/ image.size.height;
        } 
    
        CGRect rect1  = {{0, 0}, {maskImage.size.width, maskImage.size.height}};
        CGRect rect2  = {{-((image.size.width*ratio)-maskImage.size.width)/2 , -((image.size.height*ratio)-maskImage.size.height)/2}, {image.size.width*ratio, image.size.height*ratio}};
    
    
        CGContextClipToMask(mainViewContentContext, rect1, maskImageRef);
        CGContextDrawImage(mainViewContentContext, rect2, image.CGImage);
    
    
        // Create CGImageRef of the main view bitmap content, and then
        // release that bitmap context
        CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext);
        CGContextRelease(mainViewContentContext);
    
        UIImage *theImage = [UIImage imageWithCGImage:newImage];
    
        CGImageRelease(newImage);
    
        // return the image
        return theImage;
    }
    
    0 讨论(0)
  • 2020-12-23 09:19

    The image to be masked MUST be created with an alpha channel. The Alpha channel may not be created from the code.

    0 讨论(0)
提交回复
热议问题