使用NSoperation多线程异步加载图片数据
2012-09-11 14:31
357 查看
转:http://blog.csdn.net/likendsl/article/details/7553716
NSoperation是ios封装好的的实现多线程的很简单的一种方法。
定义ImageDownloader ,这个类继承NSOperation,因为需要并发,需要实现下面4个方法 :
//是否允许并发,
-(BOOL)isConcurrent ;
-(BOOL)isExecuting;
//是否已经完成,这个必须要重载,不然放在NSOperationQueue里的NSOpertaion不能正常释放。
- (BOOL)isFinished
//具体下载的方法在这里执行。
- (void)start
而对应于非并发的情况下,只需要重载main方法就好了。
头文件
[cpp]
view plaincopy
ImageDownloader.h
定义如下:
[cpp]
view plaincopy
#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
实现文件
[cpp]
view plaincopy
ImageDownloader.m
定义如下:
[cpp]
view plaincopy
#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是ios封装好的的实现多线程的很简单的一种方法。
定义ImageDownloader ,这个类继承NSOperation,因为需要并发,需要实现下面4个方法 :
//是否允许并发,
-(BOOL)isConcurrent ;
-(BOOL)isExecuting;
//是否已经完成,这个必须要重载,不然放在NSOperationQueue里的NSOpertaion不能正常释放。
- (BOOL)isFinished
//具体下载的方法在这里执行。
- (void)start
而对应于非并发的情况下,只需要重载main方法就好了。
头文件
[cpp]
view plaincopy
ImageDownloader.h
定义如下:
[cpp]
view plaincopy
#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
实现文件
[cpp]
view plaincopy
ImageDownloader.m
定义如下:
[cpp]
view plaincopy
#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优化
- Win8 在给列表绑定数据时,如果有需要 异步加载的数据时,一定要使用ObservableCollection
- Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅
- Ajax使用XML异步加载数据
- Android 使用 AsyncTask异步加载 和 接口回调 获取联网获取到的 图片 工具类
- Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
- [置顶] Android_Loader_使用LoaderManager管理Loader实现异步动态加载数据
- Volley框架学习(二)使用Volley实现异步加载图片
- asp.net 使用js分页实现异步加载数据
- 使用Loader加载异步数据
- Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
- Android 解决ListView异步加载网络数据(图片文字)出现位置错乱以及优化ListView的加载
- 异步加载图片,使用lruCache,,和sdcrad手机缓存 或手机缓存,,效果很很流程
- Xutils请求数据,加载图片基本使用方法
- Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
- 关于使用Iscroll.js异步加载数据后不能滑动到最底端的问题解决方案
- Android.异步加载图片.使用LruCache和SD卡或手机缓存
- Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅