UIImage iOS 8 mask

I am using to mask uiimages in iOS 7 using the following code which works great. But now, in iOS 8, it doesn't do anything, instead of giving me back the original image + mask, it returns me a black image.

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

CGImageRef imgRef = [image CGImage];
CGImageRef maskRef = [maskImage CGImage];
CGImageRef actualMask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                          CGImageGetHeight(maskRef),
                                          CGImageGetBitsPerComponent(maskRef),
                                          CGImageGetBitsPerPixel(maskRef),
                                          CGImageGetBytesPerRow(maskRef),
                                          CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask(imgRef, actualMask);
return [UIImage imageWithCGImage:masked];

      

}

This is how my application works: - the original image will be blurred - then a mask is applied (circle, square, any shape that is a .png image) - returns the blurred image, and the mask should crop the blurred image and see behind the original image without blurring. it works on iOS 7, but in iOS 8 the disguise code (above) doesn't work. any ideas?

+3


source to share


1 answer


Use this method to mask an image in iOS 8.0. I have also used this method in my working code and it worked great.



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

    CGImageRef imgRef  = [image CGImage];
    CGImageRef maskRef = [maskImage CGImage];


    int maskWidth      = CGImageGetWidth(maskRef);
    int maskHeight     = CGImageGetHeight(maskRef);
    //  round bytesPerRow to the nearest 16 bytes, for performance sake
    int bytesPerRow    = (maskWidth + 15) & 0xfffffff0;
    int bufferSize     = bytesPerRow * maskHeight;

    //  allocate memory for the bits
    CFMutableDataRef dataBuffer = CFDataCreateMutable(kCFAllocatorDefault, 0);
    CFDataSetLength(dataBuffer, bufferSize);

    //  the data will be 8 bits per pixel, no alpha
    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef ctx            = CGBitmapContextCreate(CFDataGetMutableBytePtr(dataBuffer),
                                                        maskWidth, maskHeight,
                                                        8, bytesPerRow, colourSpace, kCGImageAlphaNone);
    //  drawing into this context will draw into the dataBuffer.
    CGContextDrawImage(ctx, CGRectMake(0, 0, maskWidth, maskHeight), maskRef);
    CGContextRelease(ctx);

    //  now make a mask from the data.
    CGDataProviderRef dataProvider  = CGDataProviderCreateWithCFData(dataBuffer);
    CGImageRef mask                 = CGImageMaskCreate(maskWidth, maskHeight, 8, 8, bytesPerRow,
                                                        dataProvider, NULL, FALSE);

    CGDataProviderRelease(dataProvider);
    CGColorSpaceRelease(colourSpace);
    CFRelease(dataBuffer);

    CGImageRef masked = CGImageCreateWithMask(imgRef, mask);
    return [UIImage imageWithCGImage:masked];

}

      

-1


source







All Articles