CoreImage 中的模糊濾鏡
1.CoreImage 中的模糊濾鏡
1.1CoreImage是蘋果用來簡化圖片處理的框架
1.2CIImage、CIFilter與CIContext三者聯系
1.3CIGaussianBlur中可能設置的參數
2.UIImage+imageEffects的category模糊效果
3.iOS8中的UIVisualEffectView模糊效果的使用方法
一、.CoreImage 中的模糊濾鏡
- (void)coreImageBlur
{
//原始圖片
UIImage *image = [UIImage imageNamed
//CoreImage部分--------------------
//CIImage
CIImage *ciImage = [[CIImage alloc]initWithImage:image];
//CIFilter
CIFilter*blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
//將圖片輸入到濾鏡中
[blurFilter setValue
//設置模糊程序
[blurFilter setValue:@(1) forKey:@"inputRadius"];
//用業查詢濾鏡能夠設置的參數以及一引起相關的信息
NSLog(@"%@",[blurFilter attributes]);
//將處理好的圖片輸出
CIImage *outCiImage = [blurFilter valueForKey:kCIOutputImageKey];
//CIContext
CIContext *context = [CIContext contextWithOptions:nil];
//獲取CGImage句柄
CGImageRef outCGImage = [context createCGImage:outCiImage
fromRect:[outCiImage extent]];
//終於獲取到圖片
UIImage *blurImage = [UIImage imageWithCGImage:outCGImage];
//釋放CGImage句柄
CGImageRelease(outCGImage);
//---------------------------------
//
//初始化UIImageView
UIImageView *imageView = \
[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 590/2.0, 988/2.0)];
imageView.image = blurImage;
imageView.center = self.view.center;
[self.view addSubview:imageView];
}
二、UIImage+ImageEffects的category 模糊效果
1.UIImage+ImageEffects是Accelerate框架中的內容
2.UIImage+ImageEffects的模糊效果很美觀
3.改動過的UIImage+ImageEffects能夠對圖片進行局模糊
#import "UIImage+ImageEffects.h"
- (void)effectiveImages
{
UIImage *sourceImage = [UIImage imageNamed:@"normal"];
CGSize imageSize = sourceImage.size;
UIImage *blurImage = [sourceImage blurImageAtFrame:CGRectMake(0, 0, imageSize.width/2, imageSize.height )];
UIImageView *imageView = [[UIImageView alloc]initWithImage:blurImage];
imageView.center = self.view.center;
[self.view addSubview:imageView];
}
UIImage+ImageEffects 下載地址:http://download.csdn.net/detail/baitxaps/8893093三、iOS8 中 UIVisualEffectView 模糊效果的使用 1.UIVisualEffectView的模糊效果是即時渲染的 2.要註意處理在UIVisualEffectiView之上的文本顯示 3.僅僅能在iOS8以上才可以使用UIVisualEffectiView
- (void)visualEffectImage
{
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImageimageNamed:@"normal"]];
scrollView.contentSize = imageView.image.size;
scrollView.bounces = NO;
[scrollView addSubview:imageView];
[self.view addSubview:scrollView];
/*加入模糊效果*/
//1.創建模糊View
UIVisualEffectView *effectView = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffecteffectWithStyle:UIBlurEffectStyleLight ]];
//2.設定尺寸
effectView.frame = CGRectMake(0, 100, 320, 200);
//3.加入到View其中
[self.view addSubview:effectView];
//4.加入顯示文本
UILabel *label = [[UILabel alloc]initWithFrame:effectView.bounds];
label.text = @"hello world";
label.font = [UIFont systemFontOfSize:32];
label.textAlignment = NSTextAlignmentCenter;
// [effectView.contentView addSubview:label];
//5.
//加入模糊子View的UIVisualEffectView
//1.創建出子模糊View
UIVisualEffectView *subEffectView = [[UIVisualEffectView alloc]initWithEffect:[UIVibrancyEffect effectForBlurEffect:(UIBlurEffect *)effectView.effect]];
//2.設定尺寸
subEffectView.frame = effectView.bounds;
//3.將子模糊View加入到effective的contentView才幹生效
[effectView.contentView addSubview:subEffectView];
//4.加入要顯示的View來達到特殊的效果
[subEffectView.contentView addSubview:label];
}
四、設計下載圖片後自己主動模糊的控件 1.用KVO監聽下載完畢後的事件 2.在子線程中進行渲染。主線程中進行圖片的載入3.新建一個下載類,GCD看前面博客文檔GCD的封裝
@interface BlurDownloadPicView : UIView
@property (nonatomic,strong)NSString *pictureUrlString;//圖片下載地址
@property (nonatomic) UIViewContentMode contentMode;//圖片顯示方式
//開始運行
- (void)startProgress;
@end
#import "UIImage+ImageEffects.h"
#import "BlurDownloadPicView.h"
#import "GCD.h"
@interface BlurDownloadPicView()
@property (nonatomic,strong)UIImageView *imageView;
@end
@implementation BlurDownloadPicView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
//初始化控件
//最好放一個方法中
self.imageView = [[UIImageView alloc]initWithFrame:self.bounds];
self.imageView.alpha = 0.f;
[self addSubview:self.imageView];
}
return self;
}
- (void)startProgress{
if (self.pictureUrlString) {
[GCDQueue executeInGlobalQueue:^{
//創建請求
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:self.pictureUrlString]];
//由於是同步請求。會堵塞主線程
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil
error:nil];
UIImage *image = [[UIImage alloc]initWithData:data];
//對圖片進行模糊。會堵塞主線程
UIImage *blurImage = [image blurImage];
[GCDQueue executeInMainQueue:^{
[UIView animateWithDuration:1.0 animations:^{
self.imageView.alpha = 1.f;
}];
self.imageView.image = blurImage;
}];
}];
}
}
@synthesize contentMode = _contentMode;
- (void)setContentMode:(UIViewContentMode)contentMode{
_contentMode = contentMode;
self.imageView.contentMode = contentMode;
}
- (UIViewContentMode)contentMode{
return _contentMode;
}
@end
4.使用
- (void)viewDidLoad {
[super viewDidLoad];
//[self coreImageBlur];
//[self visualEffectImage];
NSString *picUrlString = @"http://t1.mmonly.cc/uploads/allimg/tuku2/14400BR6-0.jpg";
BlurDownloadPicView *blurDownLoadView = [[BlurDownloadPicView alloc]initWithFrame:self.view.bounds];
blurDownLoadView.center = self.view.center;
[self.view addSubview:blurDownLoadView];
blurDownLoadView.pictureUrlString = picUrlString;
blurDownLoadView.contentMode = UIViewContentModeScaleAspectFill;
[blurDownLoadView startProgress];
}
CoreImage 中的模糊濾鏡