[IOS 开发代码]UIImage+Blur 网络图片模糊用法

UIImage-Helpers 网络图片模糊用法

    float quality = .00001f;    float blurred = .5f;

    NSURL *url = [NSURL URLWithString:@"http://pic21.nipic.com/20120614/10230657_130343324168_2.jpg"];
    NSData *data = [[NSData alloc]initWithContentsOfURL:url];
    UIImage *img = [[UIImage alloc]initWithData:data];

    NSData *imageData = UIImageJPEGRepresentation(img, quality);
    userIcon.image = [[UIImage imageWithData:imageData] blurredImage:blurred];
//
//  UIImage+Blur.h

#import <UIKit/UIKit.h>
#import <Accelerate/Accelerate.h>
#import <QuartzCore/QuartzCore.h>

@interface UIImage (Blur)

// 0.0 to 1.0
- (UIImage*)blurredImage:(CGFloat)blurAmount;

@end
//
//  UIImage+Blur.m

#import "UIImage+Blur.h"

@implementation UIImage (Blur)

- (UIImage*)blurredImage:(CGFloat)blurAmount
{
    if (blurAmount < 0.0 || blurAmount > 1.0) {
        blurAmount = 0.5;
    }

    int boxSize = (int)(blurAmount * 40);
    boxSize = boxSize - (boxSize % 2) + 1;

    CGImageRef img = self.CGImage;

    vImage_Buffer inBuffer, outBuffer;
    vImage_Error error;

    void *pixelBuffer;

    CGDataProviderRef inProvider = CGImageGetDataProvider(img);
    CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);

    inBuffer.width = CGImageGetWidth(img);
    inBuffer.height = CGImageGetHeight(img);
    inBuffer.rowBytes = CGImageGetBytesPerRow(img);

    inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);

    pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));

    outBuffer.data = pixelBuffer;
    outBuffer.width = CGImageGetWidth(img);
    outBuffer.height = CGImageGetHeight(img);
    outBuffer.rowBytes = CGImageGetBytesPerRow(img);

    error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);

    if (!error) {
        error = vImageBoxConvolve_ARGB8888(&outBuffer, &inBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
    }

    if (error) {
#ifdef DEBUG
        NSLog(@"%s error: %zd", __PRETTY_FUNCTION__, error);
#endif

        return self;
    }

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef ctx = CGBitmapContextCreate(outBuffer.data,
                                             outBuffer.width,
                                             outBuffer.height,
                                             8,
                                             outBuffer.rowBytes,
                                             colorSpace,
                                             (CGBitmapInfo)kCGImageAlphaNoneSkipLast);

    CGImageRef imageRef = CGBitmapContextCreateImage (ctx);

    UIImage *returnImage = [UIImage imageWithCGImage:imageRef];

    CGContextRelease(ctx);
    CGColorSpaceRelease(colorSpace);

    free(pixelBuffer);
    CFRelease(inBitmapData);

    CGImageRelease(imageRef);

    return returnImage;
}

@end
时间: 2024-08-01 21:31:01

[IOS 开发代码]UIImage+Blur 网络图片模糊用法的相关文章

iOS 开发代码规范有哪些

对于刚刚入门ios的同学来说,iOS 开发代码规范是很重要的知识的,这里就给大家简单总结了一下. 一.工程规范 1.功能分类 根据所做功能的不同,分为不同的功能模块,比如登录模块,首页模块,个人模块等,根据不同的功能,代码必须要放在不同功能的文件夹下. 2.代码文件分类 不管是MVC模式,MVVM模式,或是其他设计模式,在不同的功能模块下,视图控制器(Controllers),视图(Views),模型类(Models),也必须要分别存放. 3.第三方库分类 工程中会经常使用第三方库,在引入第三方

李洪强iOS开发之 - enum与typedef enum的用法

李洪强iOS开发之 - enum与typedef enum的用法 01 - 定义枚举类型 上面我们就在ViewController.h定义了一个枚举类型,枚举类型的值默认是连续的自然数,例如例子中的TO_BE_PAID=0,//开始   那么其后的就依次为1,2,3....所以一般只需要设置枚举中第一个的值就可以. 注意: 在定义枚举类型的时候一定要定义在.h中的#imort 和€interface之间定义,位置不能错了 02 - 定义操作类型 enum和enum typedef 在IOS中的使

iOS Drawing Concepts[iOS 绘画概念]

iOS Drawing Concepts https://developer.apple.com/library/ios/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GraphicsDrawingOverview/GraphicsDrawingOverview.html High-quality graphics are an important part of your app’s user interface. Providin

IOS开发之异步加载网络图片并缓存本地实现瀑布流(二)

/* * @brief 图片加载通用函数 * @parma imageName 图片名 */ - (void)imageStartLoading:(NSString *)imageName{ NSURL *url = [NSURL URLWithString:imageName]; if([_fileUtil hasCachedImage:url]){ UIImageView *imageView = [[UIImageView alloc] init]; NSString *path = [_

iOS开发多线程篇 08 —GCD的常见用法

iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) withObject:nil afterDelay:2.0]; // 2秒后再调用self的run方法 (2)使用GCD函数 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispat

iOS开发,谓词(NSPredicate)的用法:(一)基本用法

<span style="white-space: pre;"> </span>在iOS开发中,系统提供了NSPredicate这个类给我们进行一些匹配.筛选操作,非常方便.在没有用这个类时,我们要获取两个数组中某些特定的元素时,需要写代码一一对比,但是使用了这个类,只需要三四行代码就够了. 为了演示,先定义一个person类 .h文件 #import <Foundation/Foundation.h> @interface Person : NSO

[iOS开发]WKWebView加载JS

最近项目要用webView加载js文件,挺同事说WKWebView比UIWebView更加好用,于是我今天就试试,百度一发,自己写了个demo. 先看我写的代码,然后再来看WKWebView跟UIWebView的区别: 首先,遵循这两个协议WKNavigationDelegate,WKScriptMessageHandler. 接着,获取JS文本. JS交互 - (void)getJS { NSString * js = @"window.webkit.messageHandlers.obser

[IOS 开发]TableView如何刷新指定的cell 或section

//一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2]; [tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic]; //一个cell刷新 NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0]; [tableView r

iOS开发之加载大量网络图片优化

1.概述 在IOS下通过URL读一张网络图片并不像其他编程语言那样可以直接把图片路径放到图片路径的位置就ok,而是需要我们通过一段类似流的方式去加载网络图片,接着才能把图片放入图片路径显示.比如: -(UIImage *) getImageFromURL:(NSString *)fileURL {   //NSLog(@"执行图片下载函数");       UIImage * result;       NSData * data = [NSData dataWithContentsO