+ (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;

}

 

posted on 2015-06-16 15:30  童话DY  阅读(443)  评论(0编辑  收藏  举报