使用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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM