iOS 图像处理 - 模糊图像

解决问题:将图像模糊

前提:添加 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;
}

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

参考:(网上资源,部分链接已丢失)

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

iOS 图像处理 - 模糊图像,布布扣,bubuko.com

时间: 2025-01-07 04:50:01

iOS 图像处理 - 模糊图像的相关文章

详解OS X和iOS图像处理框架Core Image

转自:http://www.csdn.net/article/2015-02-13/2823961-core-image 摘要:本 文结合实例详解了OS X和iOS图像处理框架Core Image的使用,如何通过Core Image来创建和使用iOS的内置滤镜,非常适合初学者学习.虽然示例代码是用Swift写的iOS程序,不过实现概念很容易转换到 Objective-C和OS X. 这篇文章会为初学者介绍一下Core Image,一个OS X和iOS的图像处理框架. 如果你想跟着本文中的代码学习

iOS 图像处理-剪裁图像

解决问题:按照某一长宽比例,剪裁图片的上部和下部,保留中间的内容.当然也可以自定义需要剪裁留下的区域 前提:需要添加Framework:CoreGraphics.framework 代码: - (UIImage*) crop:(UIImage*)theImage{ // Get size of current image CGSize size = [theImage size]; // Create rectangle that represents a cropped image CGFlo

iOS 图像处理 - 图像拼接

解决问题:将两个图像拼接在一起 前提:需要添加Framework:CoreGraphics.framework 源码: - (UIImage *) combine:(UIImage*)leftImage :(UIImage*)rightImage { CGFloat width = leftImage.size.width * 2; CGFloat height = leftImage.size.height; CGSize offScreenSize = CGSizeMake(width, h

iOS图像处理(一)UIImage创建图像

原文链接: iOS图像处理(一)UIImage创建图像 简书主页:http://www.jianshu.com/users/37f2920f6848 Github主页:https://github.com/MajorLMJ iOS开发者公会-技术1群 QQ群号:87440292 iOS开发者公会-技术2群 QQ群号:232702419 iOS开发者公会-议事区   QQ群号:413102158

iOS图像处理(二)Core Image介绍

原文链接: iOS图像处理(二)Core Image介绍 简书主页:http://www.jianshu.com/users/37f2920f6848 Github主页:https://github.com/MajorLMJ iOS开发者公会-技术1群 QQ群号:87440292 iOS开发者公会-技术2群 QQ群号:232702419 iOS开发者公会-议事区   QQ群号:413102158

IOS图像处理(4)坐标变化

在IOS中进行绘图都是根据点来确定位置,这里就有了坐标系的概念 在Core Graphics中坐标系的坐标原点在屏幕左下角,沿y轴正方向是向上的,而在UIKit中坐标原点在屏幕左上角,沿y轴正方向向下. 我们可以通过一个3行3列的变换矩阵对2维坐标系进行任意转换(或者通过更加简便的API),常用的转换包括移动(translate),缩放(scale)以及旋转(rotate). 1 移动 - (void)drawRect:(CGRect)rect { //获取图像上下文对象 CGContextRe

IOS图像处理(9)使用CoreImage滤镜

CoreImage是IOS5中新增框架,通过这个框架我们可以轻松地对图片进行各种特效处理 使用CoreImage的主要流程如下(需要添加CoreImage框架至项目中): 1 创建CIContext,IOS系统提供了3种方法创建CIContext a.基于CPU CIContext *context = [CIContext contextWithOption:@{kCIContextUseSoftwareRenderer:@YES}]; b.基于GPU CIContext *context =

IOS图像处理(2)绘制文本

IOS中可以通过CGContextShowTextAtPoint来绘制文字,但这个方法不支持中文字符,而且在ios7之后也不推荐使用 我们可以通过NSString的drawAtPoint以及drawInRect实现更加简单的文字绘制 - (void)drawRect:(CGRect)rect { //获取图像上下文对象 CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetRGBStrokeColor(context,

IOS图像处理(8)在PDF中绘图

参考博文 绘制到PDF则要启用pdf图形上下文,PDF图形上下文的创建使用方式跟位图图形上下文是类似的,需要注意的一点就是绘制内容到PDF时需要创建分页,每页内容的开始都要调用一次UIGraphicsBeginPDFPage方法.下面的示例演示了文本绘制和图片绘制(其他图形绘制也是类似的): - (void)viewDidLoad { [super viewDidLoad]; //沙盒路径 NSArray *pathArray = NSSearchPathForDirectoriesInDoma