图片虚化处理

最近在某些app 上面看到banner 图 的图片虚化,然后花了一点时间去处理了这个问题




#import "ViewController.h"


@interface ViewController ()

@property(nonatomic,strong)UIImageView *imageView;


@property(nonatomic,strong)UIImageView *imgView;


@end


@implementation ViewController

//懒加载

- (UIImageView *)imageView

{

    if (_imageView == nil) {

        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];

        _imageView.backgroundColor = [UIColor orangeColor];

    }


    return _imageView;

}


- (UIImageView *)imgView

{

    if (_imgView == nil) {

        _imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 300, 200, 200)];

        _imgView.backgroundColor = [UIColor orangeColor];

    }

    

    return _imgView;

}


- (void)viewDidLoad {

    [super viewDidLoad];

   [self  dealImgView1];

   [self  text];

}


- (void)dealImgView1 {


   // coreImageIOS5中新加入的一个Objective-c的框架,提供了强大高效的图像处理功能,用来对基于像素的图像进行操作与分析。iOS提供了很多强大的滤镜(Filter),现在有127种之多,随着框架的更新,这一数字会继续增加。这些Filter提供了各种各样的效果,并且还可以通过滤镜链将各种效果的Filter叠加起来,形成强大的自定义效果,如果你对该效果很满意,还可以子类化滤镜。下面将代码贴在下面,与大家分享,如果对框架不熟悉,建议阅读苹果的官方API.

    [self.view addSubview:self.imgView];

    CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"图片的名字"]];

    //CIFilter,高斯模糊滤镜

    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];

     //将图片输入到滤镜中

    [filter setValue:inputImage forKey:kCIInputImageKey];

    //设置模糊程度,默认为10,取值范围(0-100)

    [filter setValue:[NSNumber numberWithFloat:10.0] forKey:@"inputRadius"];

    //将处理好的图片输出

    CIImage *result = [filter valueForKey:kCIOutputImageKey];

    //CIImage *result=[filter outputImage];//两种方法都可以

    CIContext *context = [CIContext contextWithOptions:nil];

    //获取CGImage句柄,也就是从数据流中取出图片

    CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];

#warning result  inputImage  变化不同()

//    CGImageRef cgImage = [context createCGImage:result fromRect:[result extent]];

    //最终获取到图片

    UIImage *image = [UIImage imageWithCGImage:cgImage];

    

    //释放CGImage句柄

    CGImageRelease(cgImage);

    

    self.imgView.image=image;


}


下面是虚化的样子



为什么会出现两个呢 而且不同的虚化模式


关键是:有一句代码不一样

  //获取CGImage句柄,也就是从数据流中取出图片

    CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];

#warning result  inputImage  变化不同()

//    CGImageRef cgImage = [context createCGImage:result fromRect:[result extent]];

[inputImage  extent ]  一个是处理前的图片  一个是处理后的图片

[result extent]



附录:

//用来查询滤镜可以设置的参数以及一些相关的信息

    NSLog(@"%@",[filter attributes]);

    

 

    /*

     

     

     

     [blurFilter attributes]打印结果如下:

     //参数以及一些相关的信息,如果不做任何的设置,默认程度为10( CIAttributeDefault = 10);

     2016-08-01 17:02:44.552 钱钱钱钱钱[6935:207227] {

     "CIAttributeFilterAvailable_Mac" = "10.4";

     "CIAttributeFilterAvailable_iOS" = 6;

     CIAttributeFilterCategories =     (

     CICategoryBlur,

     CICategoryStillImage,

     CICategoryVideo,

     CICategoryBuiltIn

     );

     CIAttributeFilterDisplayName = "Gaussian Blur";

     CIAttributeFilterName = CIGaussianBlur;

     CIAttributeReferenceDocumentation = "http://developer.apple.com/cgi-bin/apple_ref.cgi?apple_ref=//apple_ref/doc/filter/ci/CIGaussianBlur";

     inputImage =     {

     CIAttributeClass = CIImage;

     CIAttributeDescription = "The image to use as an input image. For filters that also use a background image, this is the foreground image.";

     CIAttributeDisplayName = Image;

     CIAttributeType = CIAttributeTypeImage;

     };

     inputRadius =     {

     CIAttributeClass = NSNumber;

     CIAttributeDefault = 10;

     CIAttributeDescription = "The radius determines how many pixels are used to create the blur. The larger the radius, the blurrier the result.";

     CIAttributeDisplayName = Radius;

     CIAttributeIdentity = 0;

     CIAttributeMin = 0;

     CIAttributeSliderMax = 100;

     CIAttributeSliderMin = 0;

     CIAttributeType = CIAttributeTypeScalar;

     };

     }

     

     */

    

    更多的博客请登录 

http://write.blog.csdn.net/postlist/0/0/enabled/2





猜你喜欢

转载自blog.csdn.net/jq2530469200/article/details/52101459