NSOperation 的使用(下载相关) 图片和文件都是能够的 断点续传 图片逐渐显示
2014-10-20 13:05
337 查看
// // ImageDownloader.h // NSOperationTest // // Created by ydc on 11-10-29. // Copyright 2011年 __MyCompanyName__. All rights reserved. // #import <Foundation/Foundation.h> @protocol imageDownloaderDelegate; @interface ImageDownloader : NSOperation { NSURLRequest* _request; NSURLConnection* _connection; NSMutableData* _data; BOOL _isFinished; BOOL _cancelled; } - (id)initWithURLString:(NSString *)url; @property(readonly) NSData *data; @property(nonatomic, assign) id<imageDownloaderDelegate> delegate; @property(nonatomic, strong) NSObject *delPara; @property(nonatomic, strong) NSRecursiveLock *cancelledLock; @end @protocol imageDownloaderDelegate @optional //图片下载完毕的托付 - (void)imageDidFinished:(UIImage *)image para:(NSObject *)obj; @end
// // ImageDownloader.m // NSOperationTest // // Created by ydc on 11-10-29. // Copyright 2011年 __MyCompanyName__. All rights reserved. // #import "ImageDownloader.h" @implementation ImageDownloader - (id)initWithURLString:(NSString *)url { self = [self init]; if (self) { assert(url != nil); _request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]]; _data = [NSMutableData data]; } return self; } - (void)start { if (![self isCancelled]) { [NSThread sleepForTimeInterval:3]; // 以异步方式处理事件,并设置代理 _connection=[NSURLConnection connectionWithRequest:_request delegate:self]; while(_connection != nil) { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; } } } #pragma mark NSURLConnection delegate Method // 接收到数据(增量)时 - (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data { [_data appendData:data]; if (self.delegate != nil){ UIImage *img = [[UIImage alloc] initWithData:self.data] ; [_delegate imageDidFinished:img para:self.delPara]; } } - (void)connectionDidFinishLoading:(NSURLConnection*)connection { if (self.delegate != nil) { UIImage *img = [[UIImage alloc] initWithData:self.data] ; [_delegate imageDidFinished:img para:self.delPara]; } _connection=nil; } -(void)connection: (NSURLConnection *) connection didFailWithError: (NSError *) error { _connection=nil; } - (void)cancelOnRequestThread { [[self cancelledLock] lock]; if ([self isCancelled]) { [[self cancelledLock] unlock]; return; } [self willChangeValueForKey:@"isCancelled"]; _cancelled = YES; [self didChangeValueForKey:@"isCancelled"]; [[self cancelledLock] unlock]; } - (BOOL)isCancelled { BOOL result; [[self cancelledLock] lock]; result = _cancelled; [[self cancelledLock] unlock]; return result; } -(BOOL)isConcurrent { return YES; } - (BOOL)isExecuting { return _connection == nil; } - (BOOL)isFinished { return _connection == nil; } @end
@property (strong,
nonatomic)NSOperationQueue *queue;
NSString *newUrl = [NSString stringWithFormat:@"http://static2.dmcdn.net/static/video/666/645/43546666:jpeg_preview_source.jpg?%d",(int)[NSDate timeIntervalSinceReferenceDate]]; ImageDownloader *imageDownloader = [[ImageDownloader alloc]initWithURLString:newUrl]; imageDownloader.delegate = self; [self.queue addOperation:imageDownloader];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{ self.urlConnection = [NSURLConnection connectionWithRequest:request delegate:self]; }];
下载文件断点续传
NSMutableURLRequest *fileRequest = [NSMutableURLRequest requestWithURL:self.downloadURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:kFileDefaultTimeout];假设文件存在则 断点续传
_startFileSize = [[fm attributesOfItemAtPath:self.pathToFile error:nil] fileSize]; NSString *range = [NSString stringWithFormat:@"bytes=%lld-", _startFileSize]; [fileRequest setValue:range forHTTPHeaderField:@"Range"];
然后启动下载
_connection = [[NSURLConnection alloc] initWithRequest:fileRequest delegate:self startImmediately:NO]; if (self.connection) { [self.connection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; [self willChangeValueForKey:@"isExecuting"]; [self.connection start]; [self didChangeValueForKey:@"isExecuting"]; }
相关文章推荐
- NSOperation 的使用(下载相关) 图片和文件都是可以的 断点续传 图片逐渐显示
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- 使用redis文件服务器完成图片下载显示
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- 图片显示+文件下载
- strusts2文件上传下载,以及二进制图片显示问题
- 使用urlconnection下载文件或图片并保存到本地
- 文件上传下载and图片上传及前台显示
- VC6.0中使用Stream Object读取数据中流文件并显示Bmp,JPG等图片
- 注意:因博客园服务器搬迁,本BLOG有关图片、下载文件、链接等失效。请不要尝试使用可能弹出的证书。
- 使用Form验证,未登录时登录页面无法显示图片、css、js文件的解决方法
- WPF用流的方式上传/显示/下载图片文件(保存在数据库)
- 下载文件会显示loading.gif图片,下载完毕图片不消失问题解决
- (二)使用libcurl实现获取目标文件大小, 下载进度显示, 断点续传等功能
- 用于显示ORACLE_BLOB存储的图片、flash文件的JSP文件,用于直接下载文件的JSP
- VC6.0中使用Stream Object读取数据中流文件并显示Bmp,JPG等图片
- 用TreeView控件实现资源管理器(显示本地硬盘下所有文件夹和文件,并可以浏览图片)(示例代码下载)
- 用TreeView控件实现资源管理器(显示本地硬盘下所有文件夹和文件,并可以浏览图片)(示例代码下载)