使用NSoperation多线程异步加载图片数据
来源:互联网 发布:电信网络诈骗犯罪量刑 编辑:程序博客网 时间:2024/05/16 10:06
NSoperation是ios封装好的的实现多线程的很简单的一种方法。
定义ImageDownloader ,这个类继承NSOperation,因为需要并发,需要实现下面4个方法 :
//是否允许并发,
-(BOOL)isConcurrent ;
-(BOOL)isExecuting;
//是否已经完成,这个必须要重载,不然放在NSOperationQueue里的NSOpertaion不能正常释放。
- (BOOL)isFinished
//具体下载的方法在这里执行。
- (void)start
而对应于非并发的情况下,只需要重载main方法就好了。
头文件ImageDownloader.h定义如下:
#import <Foundation/Foundation.h>@protocol imageDownloaderDelegate;@interface ImageDownloader : NSOperation { NSURLRequest* _request; NSURLConnection* _connection; NSMutableData* _data;//请求的数据 BOOL _isFinished; id<imageDownloaderDelegate> delegate; NSObject *delPara;//可携带额外的参数}- (id)initWithURLString:(NSString *)url;@property (readonly) NSData *data;@property(nonatomic, assign) id<imageDownloaderDelegate> delegate;@property(nonatomic, retain) NSObject *delPara;@end@protocol imageDownloaderDelegate@optional//图片下载完成的代理方法- (void)imageDidFinished:(UIImage *)image para:(NSObject *)obj;@end
实现文件
ImageDownloader.m定义如下:
#import "ImageDownloader.h"@implementation ImageDownloader@synthesize data=_data;@synthesize delegate;@synthesize delPara;- (void)dealloc { [_request release]; _request=nil; [_data release]; _data=nil; [_connection release]; _connection=nil; [delPara release]; [super dealloc]; }- (id)initWithURLString:(NSString *)url { self = [self init]; if (self) { _request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]]; _data = [[NSMutableData data] retain]; } return self; }// 开始处理-本类的主方法- (void)start { if (![self isCancelled]) { [NSThread sleepForTimeInterval:3]; // 以异步方式处理事件,并设置代理 _connection=[[NSURLConnection connectionWithRequest:_request delegate:self]retain]; 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]; }- (void)connectionDidFinishLoading:(NSURLConnection*)connection { [_connection release], _connection=nil; NSLog(@"%s", __func__); UIImage *img = [[[UIImage alloc] initWithData:self.data] autorelease]; if (self.delegate != nil) { [delegate imageDidFinished:img para:self.delPara]; } }-(void)connection: (NSURLConnection *) connection didFailWithError: (NSError *) error{ [_connection release], _connection=nil; }-(BOOL)isConcurrent { //返回yes表示支持异步调用,否则为支持同步调用 return YES; }- (BOOL)isExecuting{ return _connection == nil; }- (BOOL)isFinished{ return _connection == nil; }
应用举例:
NSString *url =@”xxxxxxx“;
NSString*param=@”额外参数“;
ImageDownloader *downloader = [[[ImageDownloader alloc] initWithURLString:url] autorelease];
downloader.delegate = self;
downloader.delPara = param;
NSOperationQueue* queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:downloader];
#pragma delegate
- (void)imageDidFinished:(UIImage *)image para:(NSObject *)obj
{
//image,下载下来的图片;
NSString*param = (NSString *)obj;//附带的参数;
}
- 使用NSoperation多线程异步加载图片数据
- 使用NSoperation多线程异步加载图片数据
- IOS_1,ios多线程使用:NSThread和NSOperation异步加载图片
- 使用NSoperation多线程异步加载图…
- 使用NSoperation 实现异步加载图片
- iphone中使用NSoperation实现图片异步加载
- iphone中使用NSoperation实现图片异步加载
- 使用NSoperation多线程异步…
- 多线程开发之NSOperation&NSOperationQueue——异步下载图片
- 多线程开发NSOperation&NSOperationQueue——异步下载图片
- 多线程异步加载图片async_pictures
- 使用异步加载图片
- 异步加载图片等数据
- ListView数据异步加载图片和使用AsyncTask优化
- android异步的网络图片加载,支持多线程异步加载
- 多线程和同步03-图片异步加载
- android ListView 多线程异步加载Url图片
- ImageLoader(多线程异步加载和显示图片)
- VARIANT_BOOL与BOOL
- 成功开发人员须记住的8件事
- Oracle sql优化
- sqlHelper数据库类
- wubi安装Ubuntu 10.10 DVD版
- 使用NSoperation多线程异步加载图片数据
- 时间即财富:创业者浪费精力的八个错误
- 在Hadoop上运行基于RMM中文分词算法的MapReduce程序
- abap内表,标题行,工作区
- 面试时会问到的几种编码
- Publishing a RESTful Web Service with JAX-WS
- JIRA 4.4 系列安装与破解
- Android NDK R8 发布
- ABAP 内表(internal table) 标题行(header line) 工作区(work area) 简介