core image几个滤镜例子 oc版本和swift版本

前端之家收集整理的这篇文章主要介绍了core image几个滤镜例子 oc版本和swift版本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


oc版本

//万花筒模式
+ (CGImageRef) getKaleidoscope:(CIContext *)context
{
    CIImage * image =
    [CIImage imageWithCGImage:[UIImage imageNamed:@"Icon"].CGImage];
    
    CIFilter * kaleidoscope = [CIFilter filterWithName:@"CIKaleidoscope"];
    [kaleidoscope setDefaults];
    [kaleidoscope setValue:image forKey:kCIInputImageKey];
    [kaleidoscope setValue:[CIVector vectorWithX:5 Y:0] forKey:kCIInputCenterKey];
    CIImage * result = [kaleidoscope valueForKey:kCIoUtputImageKey];
    
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage:result fromRect:extent];
    
    return cgImage;
}

//凹凸变形
+ (CGImageRef) getBumpDistortion:(CIContext *)context
{
    CIImage * image =
    [CIImage imageWithCGImage:[UIImage imageNamed:@"Icon"].CGImage];
    CIFilter * bumpDistortion = [CIFilter filterWithName:@"CIBumpDistortion"];
    [bumpDistortion setDefaults];
    [bumpDistortion setValue:image forKey:kCIInputImageKey];
    [bumpDistortion setValue:[CIVector vectorWithX:10 Y:5] forKey:kCIInputCenterKey];
    [bumpDistortion setValue:@55.0f forKey:kCIInputRadiusKey];
    [bumpDistortion setValue:@(3.0f) forKey:kCIInputScaleKey];
    CIImage * result = [bumpDistortion valueForKey:kCIoUtputImageKey];
    
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage:result fromRect:extent];
    return cgImage;
}

//色调调整
+ (CGImageRef) getHueAdjust:(CIContext *)context
{
    CIImage * image = [CIImage imageWithCGImage:[UIImage imageNamed:@"Icon"].CGImage];
    CIFilter * hueAdjust = [CIFilter filterWithName:@"CIHueAdjust"];
    [hueAdjust setValue:image forKey:kCIInputImageKey];
    [hueAdjust setValue:@2.094f forKey:kCIInputAngleKey];
    CIImage * result = [hueAdjust valueForKey:kCIoUtputImageKey];
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage:result fromRect:extent];
    return cgImage;
}

//变暗
+ (CGImageRef) getGloom:(CIContext *) context
{
    CIImage * image = [CIImage imageWithCGImage:[UIImage imageNamed:@"Icon"].CGImage];
    CIFilter * gloom = [CIFilter filterWithName:@"CIGloom"];
    [gloom setDefaults];
    [gloom setValue:image forKey:kCIInputImageKey];
    [gloom setValue:@5.0f forKey:kCIInputRadiusKey];
    [gloom setValue:@1.0f forKey:kCIInputIntensityKey];
    CIImage * result = [gloom valueForKey:kCIoUtputImageKey];
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage:result fromRect:extent];
    return cgImage;
}

//深褐色调
+ (CGImageRef) getSepiaTone:(CIContext *) context
{
    CIImage * image = [CIImage imageWithCGImage:[UIImage imageNamed:@"Icon"].CGImage];
    CIFilter * sepiaTone = [CIFilter filterWithName:@"CISepiaTone"];
    [sepiaTone setDefaults];
    [sepiaTone setValue:image forKey:kCIInputImageKey];
    [sepiaTone setValue:@0.8f forKey:kCIInputIntensityKey];
    CIImage * result = [sepiaTone valueForKey:kCIoUtputImageKey];
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage:result fromRect:extent];
    return cgImage;
}

//合成
+ (CGImageRef) getAdditionCompositing:(CIContext *) context
{
    CIImage * image = [CIImage imageWithCGImage:[UIImage imageNamed:@"Icon"].CGImage];
    
    CIImage * image1 = [CIImage imageWithCGImage:[UIImage imageNamed:@"ren"].CGImage];
    
    CIFilter * accordionFoldTransition = [CIFilter filterWithName:@"CIAdditionCompositing"];
    [accordionFoldTransition setDefaults];
    [accordionFoldTransition setValue:image forKey:kCIInputImageKey];
    [accordionFoldTransition setValue:image1 forKey:kCIInputBackgroundImageKey];
    CIImage * result = [accordionFoldTransition valueForKey:kCIoUtputImageKey];
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage:result fromRect:extent];
    return cgImage;
}
//高斯模糊
+ (CGImageRef) getGaussianBlur:(CIContext *) context
{
    CIImage * image = [CIImage imageWithCGImage:[UIImage imageNamed:@"Icon"].CGImage];
    CIFilter * gaussianBlur = [CIFilter filterWithName:@"CIGaussianBlur"];
    [gaussianBlur setDefaults];
    [gaussianBlur setValue:image forKey:kCIInputImageKey];
    [gaussianBlur setValue:@5.0f forKey:kCIInputRadiusKey];
    
    CIImage * result = [gaussianBlur valueForKey:kCIoUtputImageKey];
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage:result fromRect:extent];
    return cgImage;
}

//变焦模糊
+ (CGImageRef) getZoomBlur:(CIContext *) context
{
    CIImage * image = [CIImage imageWithCGImage:[UIImage imageNamed:@"Icon"].CGImage];
    CIFilter * zoomBlur = [CIFilter filterWithName:@"CIZoomBlur"];
    [zoomBlur setDefaults];
    [zoomBlur setValue:image forKey:kCIInputImageKey];
    [zoomBlur setValue:@10.0f forKey:@"inputAmount"];
    [zoomBlur setValue:[CIVector vectorWithCGPoint:CGPointMake(10,10)] forKey:kCIInputCenterKey];
    
    CIImage * result = [zoomBlur valueForKey:kCIoUtputImageKey];
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage:result fromRect:extent];
    return cgImage;
}

//白点调整
+ (CGImageRef) getWhitePointAdjust:(CIContext *) context
{
    CIImage * image = [CIImage imageWithCGImage:[UIImage imageNamed:@"Icon"].CGImage];
    CIFilter * zoomBlur = [CIFilter filterWithName:@"CIWhitePointAdjust"];
    [zoomBlur setDefaults];
    [zoomBlur setValue:image forKey:kCIInputImageKey];
    [zoomBlur setValue:[CIColor colorWithRed:1.0 green:0.5 blue:1.0 alpha:1.0] forKey:@"inputColor"];
    
    CIImage * result = [zoomBlur valueForKey:kCIoUtputImageKey];
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage:result fromRect:extent];
    return cgImage;
}

//标签效应
+ (CGImageRef) getVignetteEffect:(CIContext *) context
{
    CIImage * image = [CIImage imageWithCGImage:[UIImage imageNamed:@"Icon"].CGImage];
    CIFilter * zoomBlur = [CIFilter filterWithName:@"CIVignetteEffect"];
    [zoomBlur setDefaults];
    [zoomBlur setValue:image forKey:kCIInputImageKey];
    [zoomBlur setValue:[CIVector vectorWithCGPoint:CGPointMake(10,10)] forKey:kCIInputCenterKey];
    [zoomBlur setValue:@0.5f forKey:@"inputFalloff"];
    [zoomBlur setValue:@1.0f forKey:@"inputIntensity"];
    [zoomBlur setValue:@100.0f forKey:@"inputRadius"];
    
    CIImage * result = [zoomBlur valueForKey:kCIoUtputImageKey];
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage:result fromRect:extent];
    return cgImage;
}

//多个效果合成
+ (CGImageRef) getMix:(CIContext *) context
{
    CIImage * image = [CIImage imageWithCGImage:[UIImage imageNamed:@"Icon"].CGImage];
    CIFilter * zoomBlur = [CIFilter filterWithName:@"CIVignetteEffect"];
    [zoomBlur setDefaults];
    [zoomBlur setValue:image forKey:kCIInputImageKey];
    [zoomBlur setValue:[CIVector vectorWithCGPoint:CGPointMake(10,10)] forKey:kCIInputCenterKey];
    [zoomBlur setValue:@0.5f forKey:@"inputFalloff"];
    [zoomBlur setValue:@1.0f forKey:@"inputIntensity"];
    [zoomBlur setValue:@100.0f forKey:@"inputRadius"];
    
    CIImage * result = [zoomBlur valueForKey:kCIoUtputImageKey];
    
    CIFilter * gaussianBlur = [CIFilter filterWithName:@"CIGaussianBlur"];
    [gaussianBlur setDefaults];
    [gaussianBlur setValue:result forKey:kCIInputImageKey];
    [gaussianBlur setValue:@5.0f forKey:kCIInputRadiusKey];
    
    result = [gaussianBlur valueForKey:kCIoUtputImageKey];
    
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage:result fromRect:extent];
    return cgImage;
}

以上介绍了几个滤镜模式,如果想查询系统内置了多少种滤镜,可以执行以下代码查看:

//查看所有内置滤镜

NSArray *filterNames=[CIFilterfilterNamesInCategory:kCICategoryBuiltIn];
    for (NSString *filterNamein filterNames) {
        CIFilter *filter=[CIFilterfilterWithName:filterName];
        NSLog(@"\rfilter:%@\rattributes:%@",filterName,[filterattributes]);
    }


下面截取控制台输出的部分滤镜消息。


就拿CIZoomBlur滤镜模式来说,打印出来的信息,可以看到,里面定义了一些属性(每种滤镜,所含有的属性会有差别,在使用的时候,特别要注意)。

属性名称可以使用定义好的结构体成员kCIInputImageKey,或者直接@"inputImage"调用,都是表示输入的图片。inputImage对应的value里面定义了这个属性是什么类型的,属性描述等。如果需要其他滤镜模式的可以去查看。


swift版本

//万花筒模式
    static func getKaleidoscope(context: CIContext) -> CGImageRef {
    
        let imageName = UIImage(named:"Icon")?.CGImage
        
        let image = CIImage(CGImage: (imageName)!)
        
        let kaleidoscope  = CIFilter(name:"CIKaleidoscope");
        kaleidoscope?.setDefaults();
        kaleidoscope?.setValue(image,forKey: kCIInputImageKey);
        kaleidoscope?.setValue(CIVector(x: 5,y: 0),forKey: kCIInputCenterKey);
        
        let result  = kaleidoscope?.valueForKey(kCIoUtputImageKey);
        
        let extent = result?.extent;
        
        let cgImage = context.createCGImage(result as! CIImage,fromRect: extent!)
        return cgImage
    }
    
    //凹凸变形
    static func getBumpDistortion(context: CIContext) -> CGImageRef {
        let imageName = UIImage(named:"Icon")?.CGImage
        let image = CIImage(CGImage: (imageName)!)
        
        let bumpDistortion = CIFilter(name: "CIBumpDistortion")
        bumpDistortion?.setDefaults()
        bumpDistortion?.setValue(image,forKey: kCIInputImageKey)
        bumpDistortion?.setValue(CIVector(x: 10,y: 5),forKey: kCIInputCenterKey)
        bumpDistortion?.setValue(55.0,forKey: kCIInputRadiusKey)
        bumpDistortion?.setValue(3.0,forKey: kCIInputScaleKey)
        let result  = bumpDistortion?.valueForKey(kCIoUtputImageKey);
        
        let extent = result?.extent;
        let cgImage = context.createCGImage(result as! CIImage,fromRect: extent!)
        return cgImage
    }
    
    //色调调整
    static func getHueAdjust(context: CIContext) -> CGImageRef {
        let imageName = UIImage(named:"Icon")?.CGImage
        let image = CIImage(CGImage: (imageName)!)
        let hueAdjust = CIFilter(name: "CIHueAdjust")
        hueAdjust?.setValue(image,forKey: kCIInputImageKey)
        hueAdjust?.setValue(2.094,forKey:kCIInputAngleKey)
        
        let result  = hueAdjust?.valueForKey(kCIoUtputImageKey);
        
        let extent = result?.extent;
        let cgImage = context.createCGImage(result as! CIImage,fromRect: extent!)
        return cgImage
    }
    
    //变暗
    static func getGloom(context: CIContext) -> CGImageRef {
        let imageName = UIImage(named:"Icon")?.CGImage
        let image = CIImage(CGImage: (imageName)!)
        
        let gloom = CIFilter(name: "CIGloom")
        gloom?.setDefaults()
        gloom?.setValue(image,forKey: kCIInputImageKey)
        gloom?.setValue(5.0,forKey: kCIInputRadiusKey)
        gloom?.setValue(1.0,forKey: kCIInputIntensityKey)
        
        let result  = gloom?.valueForKey(kCIoUtputImageKey);
        
        let extent = result?.extent;
        let cgImage = context.createCGImage(result as! CIImage,fromRect: extent!)
        return cgImage
    }
    
    //深褐色调
    static func getSepiaTone(context: CIContext) -> CGImageRef {
        let imageName = UIImage(named:"Icon")?.CGImage
        let image = CIImage(CGImage: (imageName)!)
        
        let sepiaTone = CIFilter(name: "CISepiaTone")
        sepiaTone?.setDefaults()
        sepiaTone?.setValue(image,forKey: kCIInputImageKey)
        sepiaTone?.setValue(0.8,forKey: kCIInputIntensityKey)
        
        let result  = sepiaTone?.valueForKey(kCIoUtputImageKey);
        
        let extent = result?.extent;
        let cgImage = context.createCGImage(result as! CIImage,fromRect: extent!)
        return cgImage
    }
    
    //合成
    static func getAdditionCompositing(context: CIContext) -> CGImageRef {
    
        let imageName = UIImage(named:"Icon")?.CGImage
        let image = CIImage(CGImage: (imageName)!)
        let image1 = CIImage(CGImage: (UIImage(named:"ren")?.CGImage)!)
        
        let accordionFoldTransition = CIFilter(name: "CIAdditionCompositing");
        accordionFoldTransition?.setDefaults()
        accordionFoldTransition?.setValue(image,forKey: kCIInputImageKey)
        accordionFoldTransition?.setValue(image1,forKey: kCIInputBackgroundImageKey)
        
        let result  = accordionFoldTransition?.valueForKey(kCIoUtputImageKey);
        
        let extent = result?.extent;
        let cgImage = context.createCGImage(result as! CIImage,fromRect: extent!)
        return cgImage
    }
    
    //高斯模糊
    static func getGaussianBlur(context: CIContext) -> CGImageRef {
        let imageName = UIImage(named:"Icon")?.CGImage
        let image = CIImage(CGImage: (imageName)!)
        
        let gaussianBlur = CIFilter(name: "CIGaussianBlur")
        gaussianBlur?.setDefaults()
        gaussianBlur?.setValue(image,forKey: kCIInputImageKey)
        gaussianBlur?.setValue(5.0,forKey: kCIInputRadiusKey)
        
        let result  = gaussianBlur?.valueForKey(kCIoUtputImageKey);
        
        let extent = result?.extent;
        let cgImage = context.createCGImage(result as! CIImage,fromRect: extent!)
        return cgImage
    }
    
    //变焦模糊
    static func getZoomBlur(context: CIContext) -> CGImageRef {
        let imageName = UIImage(named:"Icon")?.CGImage
        let image = CIImage(CGImage: (imageName)!)
        
        let zoomBlur = CIFilter(name: "CIZoomBlur")
        zoomBlur?.setDefaults()
        zoomBlur?.setValue(image,forKey: kCIInputImageKey)
        zoomBlur?.setValue(10.0,forKey: "inputAmount")
        zoomBlur?.setValue(CIVector(x: 10,y: 10),forKey: kCIInputCenterKey)
        
        let result  = zoomBlur?.valueForKey(kCIoUtputImageKey);
        
        let extent = result?.extent;
        let cgImage = context.createCGImage(result as! CIImage,fromRect: extent!)
        return cgImage
    }
    
    //白点调整
    static func getWhitePointAdjust(context: CIContext) -> CGImageRef {
        let imageName = UIImage(named:"Icon")?.CGImage
        let image = CIImage(CGImage: (imageName)!)
        
        let whitePointAdjust = CIFilter(name: "CIWhitePointAdjust")
        whitePointAdjust?.setDefaults()
        whitePointAdjust?.setValue(image,forKey: kCIInputImageKey)
        whitePointAdjust?.setValue(CIColor(red: 1.0,green: 0.5,blue: 1.0,alpha: 1.0),forKey: "inputColor")
        
        let result  = whitePointAdjust?.valueForKey(kCIoUtputImageKey);
        
        let extent = result?.extent;
        let cgImage = context.createCGImage(result as! CIImage,fromRect: extent!)
        return cgImage
    }
    
    //标签效应
    static func getVignetteEffect(context: CIContext) -> CGImageRef {
        let imageName = UIImage(named:"Icon")?.CGImage
        let image = CIImage(CGImage: (imageName)!)
        let vignetteEffect = CIFilter(name: "CIVignetteEffect")
        vignetteEffect?.setDefaults()
        vignetteEffect?.setValue(image,forKey: kCIInputImageKey)
        vignetteEffect?.setValue(CIVector(x: 10,forKey: kCIInputCenterKey)
        vignetteEffect?.setValue(0.5,forKey: "inputFalloff")
        vignetteEffect?.setValue(1.0,forKey: "inputIntensity")
        vignetteEffect?.setValue(100.0,forKey: "inputRadius")
        
        let result  = vignetteEffect?.valueForKey(kCIoUtputImageKey);
        
        let extent = result?.extent;
        let cgImage = context.createCGImage(result as! CIImage,fromRect: extent!)
        return cgImage
    }
    
    //多个效果合成
    static func getMix(context: CIContext) -> CGImageRef {
        let imageName = UIImage(named:"Icon")?.CGImage
        let image = CIImage(CGImage: (imageName)!)
        
        let vignetteEffect = CIFilter(name: "CIVignetteEffect")
        vignetteEffect?.setDefaults()
        vignetteEffect?.setValue(image,forKey: kCIInputImageKey)
        vignetteEffect?.setValue(0.5,forKey: "inputRadius")
        
        var result  = vignetteEffect?.valueForKey(kCIoUtputImageKey);
        
        let gaussianBlur = CIFilter(name: "CIGaussianBlur")
        gaussianBlur?.setDefaults()
        gaussianBlur?.setValue(result,forKey: kCIInputRadiusKey)
        
        result = gaussianBlur?.valueForKey(kCIoUtputImageKey);
        
        let extent = result?.extent;
        let cgImage = context.createCGImage(result as! CIImage,fromRect: extent!)
        return cgImage
    }


以上介绍了几个滤镜模式,如果想查询系统内置了多少种滤镜,可以执行以下代码查看:

//查看所有内置滤镜

let filterNames = CIFilter.filterNamesInCategory(kCICategoryBuiltIn)
        
        for filterName in filterNames {
            let filter = CIFilter(name: filterName)
            print("\rfilter:\(filterName)\rattributes:\(filter?.attributes)")
        }

下面截取控制台输出的部分滤镜消息。


就拿CIZoomBlur滤镜模式来说,打印出来的信息,可以看到,里面定义了一些属性(每种滤镜,所含有的属性会有差别,在使用的时候,特别要注意)。

属性名称可以使用定义好的结构体成员kCIInputImageKey,或者直接"inputImage"调用,都是表示输入的图片。inputImage对应的value里面定义了这个属性是什么类型的,属性描述等。如果需要其他滤镜模式的可以去查看。

工程下载地址:http://download.csdn.net/detail/xiaopingping1234567/9458261 原文链接:https://www.f2er.com/swift/324348.html

猜你在找的Swift相关文章