我使用下面的代码在iOS 7中屏蔽to图像,效果很好。但现在,在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];
}
这是我的应用程序的工作原理:-原始图像将被模糊-然后应用一个蒙版(圆形,正方形,任何形状。返回模糊的图像,遮罩应该裁剪模糊的图像,并在没有模糊的情况下看到原始图像的后面。它可以在iOS 7上工作,但在iOS 8中,关于掩码的代码(上面)不能工作。有什么想法吗?
发布于 2014-12-01 10:56:28
使用此方法在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];
}
https://stackoverflow.com/questions/26332092
复制