UIImage masking problems iOS 7

前端 未结 3 1338
暗喜
暗喜 2021-02-06 19:23

I have heavily borrowed (standard) code which applies a grayscale UIImage mask to a UIImage.

- (void) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {         


        
相关标签:
3条回答
  • 2021-02-06 19:24

    Make sure that both images (the image to be masked and the mask itself) have an alpha channel! This solved the issue for me on iOS7.

    Then you should be able to use the old code you have used for iOS6.

    0 讨论(0)
  • 2021-02-06 19:27

    The above code gives a good idea of how to approach the problem(drawing over a context) but did not work for me. The code below works both on IOS 7 and previous versions(IOS 6.x).

    +(UIImage*)maskImageExt:(UIImage *)image withMask:(UIImage *)maskImage {
        CGImageRef maskRef = maskImage.CGImage;
        CGImageRef imageRef = image.CGImage;
    
        CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                            CGImageGetHeight(maskRef),
                                            CGImageGetBitsPerComponent(maskRef),
                                            CGImageGetBitsPerPixel(maskRef),
                                            CGImageGetBytesPerRow(maskRef),
                                            CGImageGetDataProvider(maskRef), NULL, true);
    
        CGImageRef maskImageRef = CGImageCreateWithMask([image CGImage], mask);
    
        CGContextRef context = CGBitmapContextCreate(nil,
                                                     CGImageGetWidth(imageRef),
                                                     CGImageGetHeight(imageRef),
                                                     CGImageGetBitsPerComponent(imageRef),
                                                     CGImageGetBytesPerRow(imageRef),
                                                     CGImageGetColorSpace(imageRef),
                                                     CGImageGetBitmapInfo(imageRef));
        CGRect imageRect = CGRectMake(0, 0, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
        CGContextDrawImage(context, imageRect, maskImageRef);
        CGImageRef maskedImageRef = CGBitmapContextCreateImage(context);
        UIImage *maskedImage = [UIImage imageWithCGImage:maskedImageRef];
    
        CGImageRelease(mask);
        CGContextRelease(context);
        CGImageRelease(maskedImageRef);
    
        return maskedImage;
    }
    
    0 讨论(0)
  • 2021-02-06 19:39

    I found this issue on my app too and I solve it by following code.

    - (UIImage*)image:(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([handledImage CGImage], mask);
        CGImageRelease(mask);
    
        // Under iOS 7, UIImage with mask no longer applied mask when saving it.
        // We draw the image to context and obtain image from context to get the image applied mask
        CGContextRef context = CGBitmapContextCreate(nil,
                                                     CGImageGetWidth(maskedImageRef),
                                                     CGImageGetHeight(maskedImageRef),
                                                     CGImageGetBitsPerComponent(maskedImageRef),
                                                     CGImageGetBytesPerRow(maskedImageRef),
                                                     CGImageGetColorSpace(maskedImageRef),
                                                     CGImageGetBitmapInfo(maskedImageRef));
    
        CGRect imageRect = CGRectMake(0, 0, CGImageGetWidth(maskedImageRef), CGImageGetHeight(maskedImageRef));
        CGContextDrawImage(context, imageRect, maskedImageRef);
        CGImageRef imageRef = CGBitmapContextCreateImage(context);
        UIImage *maskedImage = [UIImage imageWithCGImage:imageRef];
    
        CFRelease(imageRef);
        CGContextRelease(context);
        CGImageRelease(maskedImageRef);
    
        return maskedImage;
    }
    
    0 讨论(0)
提交回复
热议问题