Simulator Screen Shot 2016年8月18日 下午2.00.24.png
Simulator Screen Shot 2016年8月18日 下午2.01.28.png
#import "ViewController.h"
@interface ViewController (){
UIImageView *imageView;
NSArray *array;
NSOperationQueue *queue;// 多線程(操作隊(duì)列)中去執(zhí)行厌蔽,而且是異步執(zhí)行的芬膝。)
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*
需求1:屏幕上邊顯示著一張圖片,在下方有一個(gè)scalorView可以滑動(dòng)
里面填充的是已經(jīng)渲染好的圖片
2.通過(guò)菊花器來(lái)加載圖片,在子線程中加載
3.圖片的渲染是帶有動(dòng)畫(huà)效果
4.子線程改變圖片的coreImage不要卡UI
ps:使用線程的時(shí)候不要使用全局變量,在for循環(huán)里面,否則只有最后一個(gè)能附上值
*/
int viewWidth=[[UIScreen mainScreen]bounds].size.width;
int viewHeight=[[UIScreen mainScreen]bounds].size.height;
queue=[[NSOperationQueue alloc]init];//創(chuàng)建操作隊(duì)列
array=@[@"CIPhotoEffectMono",@"CIPhotoEffectTonal",@"CIPhotoEffectNoir",@"CIPhotoEffectFade",@"CIPhotoEffectChrome",@"CIPhotoEffectProcess",@"CIPhotoEffectTransfer",@"CIPhotoEffectInstant",@"CISRGBToneCurveToLinear"];
CGRect imageViewRect=CGRectMake(0, 0, viewWidth, viewHeight-150);
imageView=[[UIImageView alloc]initWithFrame:imageViewRect];
imageView.image=[UIImage imageNamed:@"4.jpg"];
imageView.contentMode=UIViewContentModeScaleAspectFit;//圖片自適應(yīng)
[self.view addSubview:imageView];
CGRect scrollViewRect=CGRectMake(0, viewHeight-150, viewWidth, 150);
UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:scrollViewRect];
scrollView.backgroundColor=[UIColor orangeColor];
scrollView.contentSize=CGSizeMake(110*9-10, 130);
for (int i=0; i<9; i++) {
//photo
CGRect imageViewRect=CGRectMake((100 +10)*i, 10, 100, 130);
UIImageView *imageViewScroll=[[UIImageView alloc]initWithFrame:imageViewRect];
imageViewScroll.contentMode=UIViewContentModeScaleAspectFit;
[scrollView addSubview:imageViewScroll];
//活動(dòng)指示器
UIActivityIndicatorView *indicatorView=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
indicatorView.center=CGPointMake(imageViewScroll.frame.size.width/2, imageViewScroll.frame.size.height/2);
[indicatorView startAnimating];
[imageViewScroll addSubview:indicatorView];
//tag各單擊事件
UITapGestureRecognizer *tap= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
imageViewScroll.tag=i;
[imageViewScroll addGestureRecognizer:tap];
imageViewScroll.userInteractionEnabled=YES;
//多線程
NSBlockOperation *photo_block=[NSBlockOperation blockOperationWithBlock:^{
//加載圖片
UIImage *image=[self filterImage:array[i] originImage:[UIImage imageNamed:@"4.jpg"]];
[imageViewScroll performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
//停止菊花器
NSBlockOperation *stopIndicator=[NSBlockOperation blockOperationWithBlock:^{
[self performSelectorOnMainThread:@selector(updateIndicator:) withObject:indicatorView waitUntilDone:YES];
}];
[queue addOperation:stopIndicator];
}];
[queue addOperation:photo_block];
}
[self.view addSubview:scrollView];
}
-(void)tapAction:(UITapGestureRecognizer *)sender{
UIImageView *imageV=(UIImageView *)sender.view;
imageView.image=imageV.image;
}
//filter
-(UIImage *)filterImage:(NSString *)type originImage:(UIImage *)image{
CIContext *context=[CIContext contextWithOptions:nil];
CIImage *resultImage=[[CIImage alloc]initWithCGImage:image.CGImage];
//系統(tǒng)濾鏡
CIFilter *filter=[CIFilter filterWithName:type];
//傳入要過(guò)濾的圖片
[filter setValue:resultImage forKey:kCIInputImageKey];
//輸出過(guò)濾后圖片
resultImage =filter.outputImage;
CGRect extent=[resultImage extent];
CGImageRef cgImage=[contextcreateCGImage:resultImage fromRect:extent];
UIImage *result=[UIImage imageWithCGImage:cgImage];
return result;
}
//stop 活動(dòng)指示器
-(void)updateIndicator:(UIActivityIndicatorView *)sender{
[sender stopAnimating];
[sender removeFromSuperview];
}
@end