iOS图像模糊处理

jopen 10年前

先添加CoreGraphics.framework

代码如下:

    - (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;        }  
</div> </div> 出现问题:因为在模糊的时候,边缘会变成半透明的状态,所以理想状况是可以对原图像进行适当放大,选择使用CIAffineClamp在模糊之前对图像进行处理。

源代码如下:

    - (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;        }  
</div> </div>
参考:(网上资源,部分链接已丢失)http://stackoverflow.com/questions/12839729/correct-crop-of-cigaussianblur