Mask UIImage iOS 8



我在iOS7中使用以下代码屏蔽uiimages,效果很好。但现在,在iOS 8中,它什么都不做,它没有给我返回原始图像+掩码,而是给我返回一个黑色图像。

- (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];

}

以下是我的应用程序的工作原理:-原始图像将变得模糊-然后应用一个掩码(圆形、方形、任何形状。这就是.png图像)-返回模糊的图像,蒙版应该裁剪模糊的图像并看到原始图像的后面而不模糊。它在iOS 7上工作,但在iOS 8中,关于屏蔽的代码(上面)不起作用。有什么想法吗?

使用此方法在iOS 8.0中屏蔽图像。我在运行代码中也使用了这种方法,并且运行良好。

- (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's 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];
}

最新更新