1.继承NSOperation

DownLoadImageTask.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@protocol DownLoadImageDelegate;//声明 @interface DownLoadImageTask : NSOperation<NSURLConnectionDelegate>{
long long totalLength;
BOOL done;
} @property(nonatomic,weak) id<DownLoadImageDelegate>downloadImageDelegate;
@property(nonatomic,retain) NSMutableData *buffer;
@property(nonatomic,retain) NSURLRequest *request;
@property(nonatomic,retain) NSURLConnection *connection; - (id)initWithURLString:(NSString *)url; @end @protocol DownLoadImageDelegate
//图片下载完成的委托
-(void)imageDownLoadFinished:(UIImage *)img;
//更新图片下载进度条的值
-(void)updateDownProgress:(double) value;
@end
#import "DownLoadImageTask.h"
@implementation DownLoadImageTask
- (id)initWithURLString:(NSString *)url{
NSLog(@"url=%@",url);
self = [super init];
if(self){
self.request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
self.buffer = [NSMutableData data];
}
return self;
} //主要处理方法
-(void)start{ //或者main
NSLog(@"DownLoadImageTask-start"); if(![self isCancelled]){
//暂停一下
[NSThread sleepForTimeInterval:];
//设置connection及其代理
self.connection = [[NSURLConnection alloc]initWithRequest:self.request delegate:self];
if(self.connection!=nil){
while(!done){
[[NSRunLoop currentRunLoop]runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
}
}
} -(void)httpConnectEndWithError{
//[self hiddenWaiting];
NSLog(@"httpConnectEndWithError");
} -(void)dealloc{
self.buffer = nil;
self.connection = nil;
self.request = nil;
self.downloadImageDelegate = nil;
} #pragma NSURLConnection delegate methods
//不执行缓存
-(NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse{
return nil;
} //连接发生错误
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
[self performSelectorOnMainThread:@selector(httpConnectEndWithError) withObject:self waitUntilDone:NO];
[self.buffer setLength:];
} //收到响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if(httpResponse && [httpResponse respondsToSelector:@selector(allHeaderFields)]){
NSDictionary *httpResponseHeaderFields = [httpResponse allHeaderFields];
totalLength = [[httpResponseHeaderFields objectForKey:@"Content-Length"] longLongValue];
NSLog(@"totalLength is %lld",totalLength);
}
} //接收数据
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
//NSLog(@"didReceiveData...");
[self.buffer appendData:data]; double progressValue = totalLength==?:((double)([self.buffer length])/(double)totalLength);
//更新进度条值
//[(NSObject *)self.downloadImageDelegate performSelectorOnMainThread:@selector(updateDownProgress:) withObject:[NSNumber numberWithDouble:progressValue] waitUntilDone:NO];//主线程中执行代理方法
[self.downloadImageDelegate updateDownProgress:progressValue];
} //下载完毕
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
done = YES;
UIImage *img = [[UIImage alloc] initWithData:self.buffer];
//[(NSObject *)self.downloadImageDelegate performSelectorOnMainThread:@selector(imageDownLoadFinished:) withObject:img waitUntilDone:NO];
[self.downloadImageDelegate imageDownLoadFinished:img];
} -(BOOL)isConcurrent {
//返回yes表示支持异步调用,否则为支持同步调用
return YES; } - (BOOL)isExecuting{
return self.connection == nil;
} - (BOOL)isFinished{
return self.connection == nil;
} @end

2.在ViewController中调用NSOperation子类

ViewController.h

#import <UIKit/UIKit.h>
#import "DownLoadImageTask.h" @interface ViewController:UIViewController<DownLoadImageDelegate> @property(strong,nonatomic)NSOperationQueue *queue;
@property(strong,nonatomic)UIImageView *appImgView; @property (strong,nonatomic)UIImage *image1;
@property (strong,nonatomic)UIImage *image2; @end

ViewController.m

#import "ViewController.h"
#import "DownLoadImageTask.h" typedef void (^downLoad1)();
@interface ViewController() @property (nonatomic,strong)downLoad1 downloadBlock; @end @implementation ViewController #pragma mark - LoadView
-(void)loadView{
//初始化视图
[self initViews]; //显示等待框
[self showWaiting];
NSString *url = @"http://hiphotos.baidu.com/newwen666666/pic/item/01ec7750863e49600cf3e3cc.jpg";
DownLoadImageTask *task = [[DownLoadImageTask alloc]initWithURLString:url];
task.downloadImageDelegate = self;
//task.operationId = index++; self.queue = [[NSOperationQueue alloc]init];
[self.queue addOperation:task]; } //初始化视图组件
-(void)initViews{
CGRect frame = [UIScreen mainScreen].applicationFrame;
UIView *appView = [[UIView alloc]initWithFrame:frame];
self.view = appView;
[self.view setBackgroundColor:[UIColor colorWithWhite:1.0 alpha:1.0]];
frame = CGRectMake(, , frame.size.width, frame.size.height);
_appImgView = [[UIImageView alloc]initWithFrame:frame];
[self.view addSubview:_appImgView];
} //展示等待框
-(void)showWaiting{
CGRect frame = CGRectMake(, -, , );
int x = frame.size.width; int progressWidth = ;
int progressHeight = ;
frame = CGRectMake((x-progressWidth)/, , progressWidth, progressHeight);
UIProgressView *progress = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
progress.frame = frame;
progress.progress = 0.0;
progress.backgroundColor = [UIColor whiteColor]; UILabel *showValue = [[UILabel alloc]init];
frame = showValue.frame;
frame.origin.x = CGRectGetMaxX(progress.frame)+;
frame.origin.y = CGRectGetMinY(progress.frame);
frame.size.width = ;
frame.size.height = ;
showValue.frame = frame;
showValue.backgroundColor = [UIColor redColor];
showValue.text = @"0.0"; int progressIndWidth = ;
int progressIndHeight = ;
frame = CGRectMake((x-progressIndWidth)/, +, progressIndWidth, progressIndHeight);
UIActivityIndicatorView *progressInd = [[UIActivityIndicatorView alloc]initWithFrame:frame];
[progressInd startAnimating];
progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge; frame = CGRectMake((x-)/, ++, , );
UILabel *waitinglabel = [[UILabel alloc]initWithFrame:frame];
waitinglabel.text = @"正在下载应用程序图片...";
waitinglabel.textColor = [UIColor redColor];
waitinglabel.font = [UIFont systemFontOfSize:];
waitinglabel.backgroundColor = [UIColor clearColor]; frame = CGRectMake(, , , );
UIView *theView = [[UIView alloc]initWithFrame:frame];
theView.backgroundColor = [UIColor blackColor];
theView.alpha = 0.7; [progress setTag:];
[theView addSubview:progress];
[showValue setTag:];
[theView addSubview:showValue]; [theView addSubview:progressInd];
[theView addSubview:waitinglabel]; [theView setTag:];
[self.view addSubview:theView];
} #pragma mark - ViewDidLoad -(void)viewDidLoad{
NSLog(@"%@",self.view.subviews);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(, , , );
button.center = CGPointMake(self.view.center.x,self.view.center.y +);
[button addTarget:self action:@selector(downloadImage:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"下载图片" forState:UIControlStateNormal];
[self.view addSubview:button];
__weak typeof(self) weakSelf = self; self.downloadBlock = ^{
weakSelf.appImgView.image = weakSelf.image1;
[NSTimer scheduledTimerWithTimeInterval:2.0 target:weakSelf selector:@selector(updateImage) userInfo:nil repeats:NO];
};
} - (void)updateImage{
self.appImgView.image = self.image2;
} #pragma mark - clickAction
- (void)downloadImage:(UIButton *)sender{
[self downloadImages];
} #pragma mark - DownLoadImages - (void)downloadImages {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ); // 异步下载图片
dispatch_async(queue, ^{
// 创建一个组
dispatch_group_t group = dispatch_group_create(); __block UIImage *image1 = nil;
__block UIImage *image2 = nil; // 关联一个任务到group
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ), ^{
// 下载第一张图片
NSString *url1 = @"http://car0.autoimg.cn/upload/spec/9579/u_20120110174805627264.jpg";
image1 = [self imageWithURLString:url1];
}); // 关联一个任务到group
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ), ^{
// 下载第一张图片
NSString *url2 = @"http://hiphotos.baidu.com/lvpics/pic/item/3a86813d1fa41768bba16746.jpg";
image2 = [self imageWithURLString:url2];
}); // 等待组中的任务执行完毕,回到主线程执行block回调
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
self.image1 = image1;
self.image2 = image2;
self.downloadBlock();
// 千万不要在异步线程中自动释放UIImage,因为当异步线程结束,异步线程的自动释放池也会被销毁,那么UIImage也会被销毁
});
});
} - (UIImage *)imageWithURLString:(NSString *)urlString {
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
// 这里并没有自动释放UIImage对象
return [[UIImage alloc] initWithData:data];
} #pragma mark - DownLoadImageDelegate methods
//展示下载完毕的图片
-(void)imageDownLoadFinished:(UIImage *)img{
//退出等待框
[self hiddenWaiting];
[self.appImgView setImage:img];
NSLog(@"%@",self.view.subviews); } //更新进度条的值
-(void)updateDownProgress:(double) value{
UIProgressView *progresss = (UIProgressView *)[self.view viewWithTag:];
UILabel *showValue = (UILabel*)[self.view viewWithTag:];
progresss.progress = value;
showValue.text = [NSString stringWithFormat:@"%.1f%%",(double)(value*)];
} //隐藏等待框
-(void)hiddenWaiting{
[[self.view viewWithTag:]removeFromSuperview];
} - (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
} - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} @end

NSOperation使用的更多相关文章

  1. iOS多线程之9.自定义NSOperation

      本文主要讲如何自定义NSOperation,以及自定义NSOperation的一些注意事项,以下载图片为例. 新建一个类,继承于NSOperation. CustomOperation.h 代码 ...

  2. iOS多线程之8.NSOPeration的其他用法

      本文主要对NSOPeration的一些重点属性和方法做出介绍,以便大家可以更好的使用NSOPeration. 1.添加依赖 - (void)addDependency:(NSOperation * ...

  3. iOS多线程之7.NSOperation的初识

    NSOperation和GCD一样,不用我们管理线程的生命周期,加锁等问题,只要把操作封装进NSOperation中,系统会自动帮我们创建线程,执行操作.而且他是面向对象的,我们看起来更容易理解,使用 ...

  4. 4.4 多线程进阶篇<下>(NSOperation)

    本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人"简书" 本文源码 Demo 详见 Github https://github.c ...

  5. 认识和使用NSOperation

    原文链接:http://www.jianshu.com/p/2de9c776f226 NSOperation是OC中多线程技术的一种,是对GCD的OC包装.它包含队列(NSOperationQueue ...

  6. 多线程下NSOperation、NSBlockOperation、NSInvocationOperation、NSOperationQueue的使用

    本篇文章主要介绍下多线程下NSOperation.NSBlockOperation.NSInvocationOperation.NSOperationQueue的使用,列举几个简单的例子. 默认情况下 ...

  7. iOS NSOperation 封装 通知实现界面更新

    #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface MYOperation : NSOpe ...

  8. iOS NSOperation 异步加载图片 封装NSOperation 代理更新

    #import <Foundation/Foundation.h> @class MYOperation; @protocol MYOperationDelecate <NSObje ...

  9. 3.多线程NSOperation

    1.NSOperation的基本操作 使用NSOperation的两个子类,NSInvocationOperation 和 NSBlockOperation 创建操作,然后将操作添加到队列中去执行 / ...

  10. NSOperation的start与main,并发与非并发。

    http://blog.csdn.net/a2331046/article/details/52294006 在ios4以前,只有非并发的情况下,队列会为operation开启一个线程来执行.如果是并 ...

随机推荐

  1. [转]看了这个才发现jQuery源代码不是那么晦涩

    很多人觉得jquery.ext等一些开源js源代码 十分的晦涩,读不懂,遇到问题需要调试也很费劲.其实我个人感觉主要是有几个方面的原因: 对一些js不常用的语法.操作符不熟悉 某个function中又 ...

  2. 题目1373:整数中1出现的次数(从1到n整数中1出现的次数)

    题目1373:整数中1出现的次数(从1到n整数中1出现的次数) 题目描述: 亲们!!我们的外国友人YZ这几天总是睡不好,初中奥数里有一个题目一直困扰着他,特此他向JOBDU发来求助信,希望亲们能帮帮他 ...

  3. Sqli-LABS通关笔录-6

    第六关跟第五关一样的是布尔型盲注技术. 只是在部分有出路 添加一个单引号.程序无反应.双引号和斜杠可使其报错. 这一关让我学到了 1.管他三七二十七报错看看语句再说. THE END

  4. BZOJ 2438: [中山市选2011]杀人游戏

    Description 给你一个有向图,求至少询问多少次能够得到全部点的信息. Sol Tarjan + 强连通分量缩点 + 判断. 先缩点,如果我们知道了强连通分量里的任意一个点,我们就可以知道这些 ...

  5. HTTP协议概念篇

    1.概念 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端的浏览器. ...

  6. IDEA 编译找不到符号,文件却没有错误。

    单独编译提交找不到符号的文件. DIEAA

  7. malloc/free与new/delete的区别

    相同点:都可用于申请动态内存和释放内存 不同点:(1)操作对象有所不同.malloc与free是C++/C 语言的标准库函数,new/delete 是C++的运算符.对于非内部数据类的对象而言,光用m ...

  8. POJ 1088

    http://poj.org/problem?id=1088 一道中文题,这道题如果不限时的话,是个简单的搜索,但限时的话,就要用记忆化搜索 所谓记忆化搜索就是对每一次搜索的结果进行记录,然后之后的如 ...

  9. Google Code Jam 2015 R1C B

    题意:给出一个键盘,按键都是大写字母.给出一个目标单词和一个长度L.最大值或者最大长度都是100.现在随机按键盘,每个按键的概率相同. 敲击出一个长度为L的序列.求该序列中目标单词最多可能出现几次,期 ...

  10. ios 汉字字符串数组拼音排序

    ios没有提供简单的汉字拼音排序方法,在网上看到了oc方法,这里写以下对应的swift方法 var stringCompareBlock: (String,String)->Bool = { ( ...