码迷,mamicode.com
首页 > Windows程序 > 详细

Core Image 给图片加模糊效果 vImage API进行模糊

时间:2015-05-19 22:55:10      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

- (UIImage *)blurryImage:(UIImage *)image 
           withBlurLevel:(CGFloat)blur {
           
    CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage];
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"
                         keysAndValues:kCIInputImageKey, inputImage,
                                       @"inputRadius", @(blur), 
                                       nil];
    CIImage *outputImage = filter.outputImage;
    CGImageRef outImage = [self.context createCGImage:outputImage fromRect:[outputImage extent]];
    return [UIImage imageWithCGImage:outImage];
}


使用vImage API进行模糊

- (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur {
    if (blur < 0.f || blur > 1.f) {
        blur = 0.5f;
    }
    int boxSize = (int)(blur * 100);
    boxSize = boxSize - (boxSize % 2) + 1;
     
    CGImageRef img = image.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));
     
    if(pixelBuffer == NULL)
        NSLog(@"No pixelbuffer");
     
    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) {
        NSLog(@"error from convolution %ld", error);
    }
     
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(
                                    outBuffer.data,
                                    outBuffer.width,
                                    outBuffer.height,
                                    8,
                                    outBuffer.rowBytes,
                                    colorSpace,
                                    kCGImageAlphaNoneSkipLast);
    CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
    UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
     
    //clean up
    CGContextRelease(ctx);
    CGColorSpaceRelease(colorSpace);
     
    free(pixelBuffer);
    CFRelease(inBitmapData);
     
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(imageRef);
     
    return returnImage;
}


Core Image 给图片加模糊效果 vImage API进行模糊

标签:

原文地址:http://my.oschina.net/zdiovo/blog/416986

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!