遇到一例案例,iOS 10.1.1 使用CIBlendWithAlphaMask
渲染生成馬賽克涂抹效果時(shí)眷射,沒(méi)有渲染出來(lái)妖碉,改為GPU繪制后欧宜,倒就正常了冗茸。原因未知,暫且記錄下解決方案夏漱。
已知問(wèn)題機(jī)型:
iphone 7p iOS 10.1.1
iphone 6sp iOS 10.1.1
iphone 6 iOS 10.1.1
(iphone 5 iOS 10.1.1 沒(méi)有問(wèn)題)
原實(shí)現(xiàn):
CIImage *resultImage = [[CIFilter filterWithName:@"CIBlendWithAlphaMask"
keysAndValues:kCIInputImageKey, ciOutputImagePixellate,
kCIInputMaskImageKey, maskImage,
kCIInputBackgroundImageKey, ciInputImage, nil]
valueForKey:kCIOutputImageKey];
CIContext *ciContext = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [ciContext createCGImage:resultImage fromRect:[resultImage extent]];
CGImageRef cgImage = [ciContext createCGImage:resultImage fromRect:[resultImage extent]];
// 獲取UIImage
UIImage *filteredImage = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
修改后實(shí)現(xiàn)方案:
CIImage *resultImage = [[CIFilter filterWithName:@"CIBlendWithAlphaMask"
keysAndValues:kCIInputImageKey, ciOutputImagePixellate,
kCIInputMaskImageKey, maskImage,
kCIInputBackgroundImageKey, ciInputImage, nil]
valueForKey:kCIOutputImageKey];
EAGLContext *glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
CIContext *ciContext = [CIContext contextWithEAGLContext:glContext
options:@{kCIContextWorkingColorSpace : [NSNull null]}];
CGImageRef cgImage = [ciContext createCGImage:resultImage fromRect:[resultImage extent]];
// 獲取UIImage
UIImage *filteredImage = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
附上圖片部分馬賽克的源碼:
- (UIImage *)demoCoreImageFilter {
UIImage *originImage = [UIImage imageNamed:@"facetest"];
// 導(dǎo)入CIImage
CIImage *ciInputImage = [[CIImage alloc] initWithImage:originImage];
// 創(chuàng)建CIFilter
CIFilter *filterPixellate = [CIFilter filterWithName:@"CIPixellate"];
[filterPixellate setValue:ciInputImage forKey:kCIInputImageKey];
[filterPixellate setDefaults];
[filterPixellate setValue:@32 forKey:@"inputScale"];
NSLog(@"filterPixellate : %@", filterPixellate.attributes);
// 獲取濾鏡效果之后的CIImage
CIImage *ciOutputImagePixellate = [filterPixellate valueForKey:kCIOutputImageKey];
// 人臉識(shí)別
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace
context:nil
options:nil];
NSArray *faceArray = [detector featuresInImage:ciInputImage options:nil];
// Create a green circle to cover the rects that are returned.
CIImage *maskImage = nil;
for (CIFeature *f in faceArray) {
CGFloat centerX = f.bounds.origin.x + f.bounds.size.width / 2.0;
CGFloat centerY = f.bounds.origin.y + f.bounds.size.height / 2.0;
CGFloat radius = MIN(f.bounds.size.width, f.bounds.size.height) / 1.5;
CIFilter *radialGradient = [CIFilter filterWithName:@"CIRadialGradient" withInputParameters:@{
@"inputRadius0": @(radius),
@"inputRadius1": @(radius + 1.0f),
@"inputColor0": [CIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0],
@"inputColor1": [CIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0],
kCIInputCenterKey: [CIVector vectorWithX:centerX Y:centerY],
}];
CIImage *circleImage = [radialGradient valueForKey:kCIOutputImageKey];
if (nil == maskImage)
maskImage = circleImage;
else
maskImage = [[CIFilter filterWithName:@"CISourceOverCompositing" withInputParameters:@{
kCIInputImageKey: circleImage,
kCIInputBackgroundImageKey: maskImage,
}] valueForKey:kCIOutputImageKey];
}
CIImage *resultImage = [[CIFilter filterWithName:@"CIBlendWithAlphaMask"
keysAndValues:kCIInputImageKey, ciOutputImagePixellate,
kCIInputMaskImageKey, maskImage,
kCIInputBackgroundImageKey, ciInputImage, nil]
valueForKey:kCIOutputImageKey];
EAGLContext *glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
CIContext *ciContext = [CIContext contextWithEAGLContext:glContext
options:@{kCIContextWorkingColorSpace : [NSNull null]}];
CGImageRef cgImage = [ciContext createCGImage:resultImage fromRect:[resultImage extent]];
// 獲取UIImage
UIImage *filteredImage = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
return filteredImage;
}
代碼講解:
- 將原圖馬賽克化豪诲,得到一張馬賽克后的圖片 ciOutputImagePixellate
- 對(duì)原圖人臉識(shí)別,并取識(shí)別后的人臉區(qū)域 maskImage
- 使用 CIBlendWithAlphaMask 濾鏡進(jìn)行合成
- 指定GPU 渲染