Blur Images (blur background)
Blur Images
Background blur is becoming more and more common. How can we use code to implement it? In the previous article, we discussed the knowledge about CoreImage,
Of course, first try to use CoreImage to solve the problem. From all the supported filters printed last time, among the 127 filters, there is a Blur keyword, onlyCIGaussianBlur
(Gaussian Blur), and the fatal flaw of this fuzzy blur is that there will be white edges (Art-savvy people may know that this is the reason for bitmap and vector map)
Gaussian fuzzy code
CIContext *context = [CIContext contextWithOptions:nil]; CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"1.png"]]; // create gaussian blur filter CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"]; [filter setValue:inputImage forKey:kCIInputImageKey]; [filter setValue:[NSNumber numberWithFloat:10.0] forKey:@"inputRadius"]; // blur image CIImage *result = [filter valueForKey:kCIOutputImageKey]; CGImageRef cgImage = [context createCGImage:result fromRect:[result extent]]; UIImage *image = [UIImage imageWithCGImage:cgImage]; CGImageRelease(cgImage); self.mainImageView.image = image;
So according to my current knowledge, using CoreImage cannot solve the problem...
By searching for references, we found that vImage API is available in iOS5.0, which belongs to Accelerate. Framework. Therefore, if you want to use it, you must add this Framework to the project. The fuzzy algorithm uses the vImageBoxConvolve_ARGB8888 function.
-(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;}
Final effect: