iOS圖片模糊效果的實(shí)現(xiàn)方法
本文為大家分享了iOS圖片模糊效果的三種實(shí)現(xiàn)方式,供大家參考,具體內(nèi)容如下
1.實(shí)現(xiàn)效果依次如圖:原圖、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 使用系統(tǒng)自帶的處理方式
- (void)iOS8BlurImageImplement {
UIBlurEffect *beffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *view = [[UIVisualEffectView alloc] initWithEffect:beffect];
view.frame = self.view.bounds;
[self.view addSubview:view];
}
// 使用CoreImage實(shí)現(xiàn)圖片模糊
- (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實(shí)現(xiàn)圖片模糊
// iOS5.0中新增了vImage API可以使用,它屬于Accelerate.Framework,所以如果你要使用它要在工程中加入這個(gè)Framework。模糊算法使用的是vImageBoxConvolve_ARGB8888這個(gè)函數(shù)。
- (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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
UIScrollView實(shí)現(xiàn)六棱柱圖片瀏覽效果
這篇文章主要為大家介紹了UIScrollView實(shí)現(xiàn)六棱柱圖片瀏覽效果示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
iOS點(diǎn)擊推送消息跳到應(yīng)用指定頁(yè)面方法
現(xiàn)在的推送用的越來(lái)越頻繁,幾乎每個(gè)應(yīng)用都開始用到了。這篇文章主要介紹了iOS點(diǎn)擊推送消息跳到應(yīng)用指定頁(yè)面方法,有需要的可以了解一下。2016-11-11
iOS中UIActionSheet動(dòng)態(tài)添加按鈕
這篇文章主要介紹了iOS中UIActionSheet動(dòng)態(tài)添加按鈕功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06
詳解iOS開發(fā)中UItableview控件的數(shù)據(jù)刷新功能的實(shí)現(xiàn)
這篇文章主要介紹了詳解iOS開發(fā)中UItableview控件的數(shù)據(jù)刷新功能的實(shí)現(xiàn),代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12
詳解iOS開發(fā)中app的歸檔以及偏好設(shè)置的存儲(chǔ)方式
這篇文章主要介紹了iOS開發(fā)中app的歸檔以及偏好設(shè)置的存儲(chǔ)方式,示例代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12
IOS 開發(fā)之讀取addressbook的實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了IOS 開發(fā)之讀取addressbook的實(shí)現(xiàn)實(shí)例的相關(guān)資料,希望通過本文大家能夠掌握這樣的內(nèi)容,需要的朋友可以參考下2017-09-09

