美文网首页
iOS 滤镜之CIBlendWithAlphaMask在特殊系统

iOS 滤镜之CIBlendWithAlphaMask在特殊系统

作者: bai_ya | 来源:发表于2018-05-29 23:10 被阅读49次

    遇到一例案例,iOS 10.1.1 使用CIBlendWithAlphaMask 渲染生成马赛克涂抹效果时,没有渲染出来,改为GPU绘制后,倒就正常了。原因未知,暂且记录下解决方案。

    已知问题机型:

    iphone 7p   iOS 10.1.1
    iphone 6sp  iOS 10.1.1
    iphone 6        iOS 10.1.1
    (iphone 5 iOS 10.1.1 没有问题)
    

    原实现:

    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);
    
    

    修改后实现方案:

    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"];
        // 导入CIImage
        CIImage *ciInputImage = [[CIImage alloc] initWithImage:originImage];
        
        // 创建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];
        
        
        // 人脸识别
        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;
    }
    
    

    代码讲解:

    1. 将原图马赛克化,得到一张马赛克后的图片 ciOutputImagePixellate
    2. 对原图人脸识别,并取识别后的人脸区域 maskImage
    3. 使用 CIBlendWithAlphaMask 滤镜进行合成
    4. 指定GPU 渲染

    参考链接

    Anonymous Faces Filter Recipe

    相关文章

      网友评论

          本文标题:iOS 滤镜之CIBlendWithAlphaMask在特殊系统

          本文链接:https://www.haomeiwen.com/subject/isyzjftx.html