本文為年夜家分享了IOS圖片隱約後果的三種完成方法,供年夜家參考,詳細內容以下
1.完成後果順次如圖:原圖、IOS8後果、Core Image後果、 VImage 後果
-
2. 代碼
#import "ViewController.h" #import <Accelerate/Accelerate.h> @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]]; // [self IOS8BlurImageImplement]; // [self coreImageImplement]; [self vImageImplement]; } // iOS8 應用體系自帶的處置方法 - (void)iOS8BlurImageImplement { UIBlurEffect *beffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; UIVisualEffectView *view = [[UIVisualEffectView alloc] initWithEffect:beffect]; view.frame = self.view.bounds; [self.view addSubview:view]; } // 應用CoreImage完成圖片隱約 - (void)coreImageImplement{ CIContext *context = [CIContext contextWithOptions:nil]; NSError *error = nil; NSString *filePath = [[NSBundle mainBundle] pathForResource:@"background" ofType:@"png"]; NSData *imageData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingUncached error:&error]; //NSData *imageData = [NSData dataWithContentsOfFile:@"background.png"]; CIImage *image = [CIImage imageWithData:imageData]; CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"]; [filter setValue:image forKey:kCIInputImageKey]; [filter setValue:@2.0f forKey:@"inputRadius"]; CIImage *result = [filter valueForKey:kCIOutputImageKey]; CGImageRef outImage = [context createCGImage:result fromRect:[result extent]]; UIImage *bluerImage = [UIImage imageWithCGImage:outImage]; UIImageView *imageView = [[UIImageView alloc] initWithImage:bluerImage]; imageView.frame = self.view.bounds; [self.view addSubview:imageView]; } // 應用vImage API完成圖片隱約 // iOS5.0中新增了vImage API可使用,它屬於Accelerate.Framework,所以假如你要應用它要在工程中參加這個Framework。隱約算法應用的是vImageBoxConvolve_ARGB8888這個函數。 - (void)vImageImplement { UIImage *image = [UIImage imageNamed:@"background"]; UIImage *blurImage = [self blurryImage:image withBlurLevel:0.5]; self.view.backgroundColor = [UIColor colorWithPatternImage:blurImage]; } - (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; voidvoid *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; } @end
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐本站。
【iOS圖片隱約後果的完成辦法】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!