使用CoreImage教程

简介:

使用CoreImage教程

CoreImage包含有很多实用的滤镜,专业处理图片的库,为了能看到各种渲染效果,请使用如下图片素材.

 

现在可以开始教程了:


#define FIX_IMAGE(image)  fixImageWidth(image, 320.f)

// 固定图片的宽度
UIImage * fixImageWidth(UIImage *image, CGFloat width)
{
    float newHeight = image.size.height * (width / image.size.width);
    CGSize size = CGSizeMake(width, newHeight);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextTranslateCTM(context, 0.0, size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    CGContextSetBlendMode(context, kCGBlendModeCopy);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height),
                       image.CGImage);
    
    UIImage *imageOut = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return imageOut;
}

代码片段


// 将UIImage转换成CIImage
    CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];
    
    // 创建滤镜
    CIFilter *filter = [CIFilter filterWithName:@"CIPhotoEffectMono"
                                  keysAndValues:kCIInputImageKey, ciImage, nil];
    [filter setDefaults];
    
    // 获取绘制上下文
    CIContext *context = [CIContext contextWithOptions:nil];
    
    // 渲染并输出CIImage
    CIImage *outputImage = [filter outputImage];
    
    // 创建CGImage句柄
    CGImageRef cgImage = [context createCGImage:outputImage
                                       fromRect:[outputImage extent]];
    
    // 获取图片
    UIImage *showImage = [UIImage imageWithCGImage:cgImage];
    
    // 释放CGImage句柄
    CGImageRelease(cgImage);

    // 显示图片
    UIImageView *imageView = \
    [[UIImageView alloc] initWithImage:FIX_IMAGE(showImage)];
    [self.view addSubview:imageView];

代码片段

效果如下:

我们对操作进行简易的封装:

CIFilterEffect.h + CIFilterEffect.m

//
//  CIFilterEffect.h
//  CIFilter
//
//  Created by YouXianMing on 14-5-9.
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface CIFilterEffect : NSObject

@property (nonatomic, strong, readonly) UIImage *result;

- (instancetype)initWithImage:(UIImage *)image filterName:(NSString *)name;

@end


//
//  CIFilterEffect.m
//  CIFilter
//
//  Created by YouXianMing on 14-5-9.
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "CIFilterEffect.h"

@interface CIFilterEffect ()

@property (nonatomic, strong, readwrite) UIImage *result;

@end

@implementation CIFilterEffect

- (instancetype)initWithImage:(UIImage *)image filterName:(NSString *)name
{
    self = [super init];
    if (self)
    {
        // 将UIImage转换成CIImage
        CIImage *ciImage = [[CIImage alloc] initWithImage:image];
        
        // 创建滤镜
        CIFilter *filter = [CIFilter filterWithName:name
                                      keysAndValues:kCIInputImageKey, ciImage, nil];
        [filter setDefaults];
        
        // 获取绘制上下文
        CIContext *context = [CIContext contextWithOptions:nil];
        
        // 渲染并输出CIImage
        CIImage *outputImage = [filter outputImage];
        
        // 创建CGImage句柄
        CGImageRef cgImage = [context createCGImage:outputImage
                                           fromRect:[outputImage extent]];
        
        _result = [UIImage imageWithCGImage:cgImage];
        
        // 释放CGImage句柄
        CGImageRelease(cgImage);
    }
    return self;
}

@end

我们来开始尝试其他的滤镜效果,我们可以尝试的至少有这些:

@"CILinearToSRGBToneCurve",
@"CIPhotoEffectChrome",
@"CIPhotoEffectFade",
@"CIPhotoEffectInstant",
@"CIPhotoEffectMono",
@"CIPhotoEffectNoir",
@"CIPhotoEffectProcess",
@"CIPhotoEffectTonal",
@"CIPhotoEffectTransfer",
@"CISRGBToneCurveToLinear",
@"CIVignetteEffect",

下面是所有渲染出来的图片,与上面提供的滤镜名字一一对应:

 

以下效果是需要进行一些配置才能达到的效果,这个就不开源了,你懂得:).

 

 

福利:

Core Image Filter Reference

https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CoreImageFilterReference/Reference/reference.html

CICategoryBlur

CICategoryColorAdjustment

CICategoryColorEffect(我们刚刚用到的一些效果在这里哦)

CICategoryCompositeOperation

CICategoryDistortionEffect

CICategoryGenerator

CICategoryGeometryAdjustment

CICategoryGradient

CICategoryHalftoneEffect

CICategoryReduction

CICategorySharpen

CICategoryStylize

CICategoryTileEffect

CICategoryTransition

 

目录
相关文章
|
C++ 容器
学习C++笔记430
C++ STL 教程
84 0
|
Shell C++ Python
学习C++笔记405
C++ Web 编程
86 0
|
C++
学习C++笔记396
C++ 多线程
99 0
|
C++
学习C++笔记382
C++ 预处理器
85 0
|
C++
学习C++笔记381
C++ 预处理器
77 0
|
C++
学习C++笔记339
C++ 异常处理
81 0
|
C++
学习C++笔记328
C++ 文件和流
99 0
|
算法 C++
学习C++笔记312
C++ 数据抽象
88 0
|
C++
学习C++笔记295
C++ 重载运算符和重载函数
89 0
|
C++
学习C++笔记292
C++ 继承
90 0
下一篇
无影云桌面