使用NSoperation多线程异步加载图片数据
2012-05-10 14:41
441 查看
NSoperation是ios封装好的的实现多线程的很简单的一种方法。
定义ImageDownloader ,这个类继承NSOperation,因为需要并发,需要实现下面4个方法 :
//是否允许并发,
-(BOOL)isConcurrent ;
-(BOOL)isExecuting;
//是否已经完成,这个必须要重载,不然放在NSOperationQueue里的NSOpertaion不能正常释放。
- (BOOL)isFinished
//具体下载的方法在这里执行。
- (void)start
而对应于非并发的情况下,只需要重载main方法就好了。
头文件
#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;//附带的参数;
}
定义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多线程异步加载图片数据
- ListView数据异步加载图片和使用AsyncTask优化
- Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
- Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
- 24.使用getJSON()方法异步加载JSON格式数据
- Android控件使用—Listview加载网络数据、图片并跳转传值
- Cocos2d-js 开发记录:图片数据资源等的异步加载
- Android异步下载网络图片&android解析xml文件的方式&使用Adapter为ListView提供数据
- Android批量图片加载经典系列——使用xutil框架缓存、异步加载网络图片
- 如何使用PHP+jQuery+MySQL实现异步加载ECharts地图数据(附源码下载)
- Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅
- Android中ListView使用- 网络图片的异步加载
- 使用Android-Universal-Image-Loader异步加载图片遇到的问题
- Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
- ztree插件的使用及列表项拖拽的实现(jQuery)+异步加载节点数据
- 使用getJSON()方法异步加载JSON格式数据
- Android使用ImageLoader异步加载网络图片(二)结合listview
- 使用okhttp加载服务器数据,Glide加载图片
- Android图片异步加载之Android-Universal-Image-Loader使用