https://github.com/YouXianMing

iOS8 CIGlassDistortion滤镜的使用

iOS8 CIGlassDistortion滤镜的使用

此为CoreImage滤镜的使用

素材

效果

混合用图片

源码:

//
//  ViewController.m
//  CIGlass
//
//  Created by XianMingYou on 15/3/15.
//  Copyright (c) 2015年 XianMingYou. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIImage *orgImage;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置背景色
    self.view.backgroundColor = [UIColor blackColor];
    
    
    self.orgImage = [UIImage imageNamed:@"bg.png"];
    
    
    // 数据源 + 设置
    CIImage *ciImage = [[CIImage alloc] initWithImage:self.orgImage];
    NSDictionary *params = @{
                             kCIInputImageKey: ciImage,
                             };
    
    // 初始化滤镜
    CIFilter *filter = [CIFilter filterWithName:@"CIGlassDistortion"
                            withInputParameters:params];
    [filter setDefaults];
    

    // 输入变形参数
    if ([filter respondsToSelector:NSSelectorFromString(@"inputTexture")]) {
        CIImage *ciTextureImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"grassdistortion.png"]];
        [filter setValue:ciTextureImage forKey:@"inputTexture"];
    }
    
    // 创建上下文 + 输出图片
    CIContext *context   = [CIContext contextWithOptions:nil];
    CIImage *outputImage = [filter outputImage];
    
    // 获取图片
    CGImageRef cgImage   = [context createCGImage:outputImage
                                         fromRect:[outputImage extent]];
    
    // 获取图片
    UIImage *image = [UIImage imageWithCGImage:cgImage];
    
    // 释放资源
    CGImageRelease(cgImage);
    
    // 加载图片
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame        = self.view.bounds;
    [self.view addSubview:imageView];
}

@end

需要注意的细节:

 

posted @ 2015-03-15 16:22  YouXianMing  阅读(690)  评论(1编辑  收藏  举报