码迷,mamicode.com
首页 > 移动开发 > 详细

IOS 毛玻璃化图片

时间:2015-06-16 16:39:09      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

+ (UIImage *)blurWithCoreImage:(UIImage *)sourceImage withFrame:(CGRect)frame

{

    CIImage *inputImage = [CIImage imageWithCGImage:sourceImage.CGImage];

    

    // Apply Affine-Clamp filter to stretch the image so that it does not look shrunken when gaussian blur is applied

    CGAffineTransform transform = CGAffineTransformIdentity;

    CIFilter *clampFilter = [CIFilter filterWithName:@"CIAffineClamp"];

    [clampFilter setValue:inputImage forKey:@"inputImage"];

    [clampFilter setValue:[NSValue valueWithBytes:&transform objCType:@encode(CGAffineTransform)] forKey:@"inputTransform"];

    

    // Apply gaussian blur filter with radius of 30

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

    [gaussianBlurFilter setValue:clampFilter.outputImage forKey: @"inputImage"];

    [gaussianBlurFilter setValue:@3 forKey:@"inputRadius"];

    

    CIContext *context = [CIContext contextWithOptions:nil];

    CGImageRef cgImage = [context createCGImage:gaussianBlurFilter.outputImage fromRect:[inputImage extent]];

    

    // Set up output context.

    UIGraphicsBeginImageContext(frame.size);

    CGContextRef outputContext = UIGraphicsGetCurrentContext();

    CGContextScaleCTM(outputContext, 1.0, -1.0);

    CGContextTranslateCTM(outputContext, 0, - frame.size.height);

    

    // Draw base image.

    CGContextDrawImage(outputContext, frame, cgImage);

    

    // Apply white tint

    CGContextSaveGState(outputContext);

    CGContextSetFillColorWithColor(outputContext, [UIColor colorWithWhite:1 alpha:0.2].CGColor);

    CGContextFillRect(outputContext, frame);

    CGContextRestoreGState(outputContext);

    

    // Output image is ready.

    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();

    CGImageRelease(cgImage);

    

    UIGraphicsEndImageContext();

    

    return outputImage;

}

 

IOS 毛玻璃化图片

标签:

原文地址:http://www.cnblogs.com/417460188dy/p/4580925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!