iOS -- 图片虚化,模糊化

首先需要导入1个类代码如下:

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) {

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

        }

    }

    

    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);

    

    CGColorSpaceRelease(colorSpace);

    CGImageRelease(imageRef);

    

    return returnImage;

}

@end


用法:

再viewController里写如下代码

#import "ViewController.h"

#import "UIImage+Blur.h"

@interface ViewController ()

{

    UIImageView *imageViewNormal;

    UIImageView *imageViewBlurred;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    imageViewNormal = [[UIImageView alloc]initWithFrame:CGRectMake(0, 30, 320, 200)];

    imageViewNormal.image = [UIImage imageNamed:@"bg.jpg"];

    //imageViewNormal.backgroundColor = [UIColor redColor];

    [self.view addSubview:imageViewNormal];

    

    imageViewBlurred = [[UIImageView alloc] initWithFrame:CGRectMake(0, 240, 320, 200)];

    

    float quality = .00001f;

    float blurred = .5f;

    

    NSData *imageData = UIImageJPEGRepresentation([imageViewNormal image], quality);

    UIImage *blurredImage = [[UIImage imageWithData:imageData] blurredImage:blurred];

    imageViewBlurred.image = blurredImage;

    [self.view addSubview:imageViewBlurred];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end


效果如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值