IOS image processing-fuzzy images

Source: Internet
Author: User
Tags xform

IOS image processing-fuzzy images

Solution:Blur an image

Prerequisites:Add CoreGraphics. framework

Source code:

- (UIImage*) blur:(UIImage*)theImage{    // create our blurred image    CIContext *context = [CIContext contextWithOptions:nil];    CIImage *inputImage = [CIImage imageWithCGImage:theImage.CGImage];        // setting up Gaussian Blur (could use one of many filters offered by Core Image)    CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];    [blurFilter setValue:inputImage forKey:kCIInputImageKey];    [blurFilter setValue:[NSNumber numberWithFloat:10.0f] forKey:@"inputRadius"];    CIImage *result = [blurFilter valueForKey:kCIOutputImageKey];        // CIGaussianBlur has a tendency to shrink the image a little,    // this ensures it matches up exactly to the bounds of our original image    CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];        UIImage *returnImage = [UIImage imageWithCGImage:cgImage];    //create a UIImage for this function to "return" so that ARC can manage the memory of the blur...    //ARC can't manage CGImageRefs so we need to release it before this function "returns" and ends.    CGImageRelease(cgImage);//release CGImageRef because ARC doesn't manage this on its own.        return returnImage;}
Problem:Because the edges become translucent during blur, the ideal situation is that the original image can be properly enlarged and CIAffineClamp can be used to process the image before blurring.

Source code:

- (UIImage*) blur:(UIImage*)theImage{    // create our blurred image    CIContext *context = [CIContext contextWithOptions:nil];    CIImage *inputImage = [CIImage imageWithCGImage:theImage.CGImage];        CIFilter *affineClampFilter = [CIFilter filterWithName:@"CIAffineClamp"];    CGAffineTransform xform = CGAffineTransformMakeScale(1.0, 1.0);    [affineClampFilter setValue:inputImage forKey:kCIInputImageKey];    [affineClampFilter setValue:[NSValue valueWithBytes:&xform                                               objCType:@encode(CGAffineTransform)]                                                 forKey:@"inputTransform"];        CIImage *extendedImage = [affineClampFilter valueForKey:kCIOutputImageKey];        // setting up Gaussian Blur (could use one of many filters offered by Core Image)    CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];    [blurFilter setValue:extendedImage forKey:kCIInputImageKey];    [blurFilter setValue:[NSNumber numberWithFloat:10.0f] forKey:@"inputRadius"];    CIImage *result = [blurFilter valueForKey:kCIOutputImageKey];        // CIGaussianBlur has a tendency to shrink the image a little,    // this ensures it matches up exactly to the bounds of our original image    CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];        UIImage *returnImage = [UIImage imageWithCGImage:cgImage];    //create a UIImage for this function to "return" so that ARC can manage the memory of the blur...    //ARC can't manage CGImageRefs so we need to release it before this function "returns" and ends.    CGImageRelease(cgImage);//release CGImageRef because ARC doesn't manage this on its own.        return returnImage;}

Reference: (online resources, some links are lost)

Http://stackoverflow.com/questions/12839729/correct-crop-of-cigaussianblur

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.