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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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, y: 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: "inputFalloff") vignetteEffect?.setValue(1.0, forKey: "inputIntensity") vignetteEffect?

.setValue(100.0, forKey: "inputRadius") var result = vignetteEffect?.valueForKey(kCIOutputImageKey); let gaussianBlur = CIFilter(name: "CIGaussianBlur") gaussianBlur?

.setDefaults() gaussianBlur?.setValue(result, forKey: kCIInputImageKey) gaussianBlur?

.setValue(5.0, 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里面定义了这个属性是什么类型的。属性描写叙述等。假设须要其它滤镜模式的能够去查看。


project下载地址:http://download.csdn.net/detail/xiaopingping1234567/9458261











以上是关于core image几个滤镜样例 oc版本号和swift版本号的主要内容,如果未能解决你的问题,请参考以下文章

绘图与滤镜全面解析--Quartz 2D Core Image

iOS学习笔记9-Core Image

python 检查python的版本号和几个包

Quartz Composer - 如何为Core Image过滤器指定目标大小?

详解OS X和iOS图像处理框架Core Image

隐藏Nginx版本号和软件名