zoukankan      html  css  js  c++  java
  • 使用NSoperation多线程异步加载图片数据

    转:http://blog.csdn.net/likendsl/article/details/7553716

    NSoperation是ios封装好的的实现多线程的很简单的一种方法。


    定义ImageDownloader ,这个类继承NSOperation,因为需要并发,需要实现下面4个方法 :

    //是否允许并发,
    -(BOOL)isConcurrent ;
    -(BOOL)isExecuting;

    //是否已经完成,这个必须要重载,不然放在NSOperationQueue里的NSOpertaion不能正常释放。
    - (BOOL)isFinished

    //具体下载的方法在这里执行。
    - (void)start 

     

    而对应于非并发的情况下,只需要重载main方法就好了。

    头文件
    1. ImageDownloader.h  
    定义如下:
    1. #import <Foundation/Foundation.h>  
    2.   
    3. @protocol imageDownloaderDelegate;  
    4.   
    5. @interface ImageDownloader : NSOperation   
    6. {  
    7.     NSURLRequest* _request;  
    8.       
    9.     NSURLConnection* _connection;  
    10.       
    11.     NSMutableData* _data;//请求的数据  
    12.       
    13.     BOOL _isFinished;   
    14.       
    15.     id<imageDownloaderDelegate> delegate;  
    16.       
    17.     NSObject *delPara;//可携带额外的参数  
    18. }  
    19.   
    20. - (id)initWithURLString:(NSString *)url;  
    21.   
    22. @property (readonly) NSData *data;  
    23. @property(nonatomic, assign) id<imageDownloaderDelegate> delegate;  
    24. @property(nonatomic, retain) NSObject *delPara;  
    25.   
    26. @end  
    27.   
    28. @protocol imageDownloaderDelegate  
    29.   
    30. @optional  
    31.   
    32. //图片下载完成的代理方法  
    33. - (void)imageDidFinished:(UIImage *)image para:(NSObject *)obj;  
    34.   
    35. @end  

    实现文件

    1. ImageDownloader.m  
    定义如下:
    1. #import "ImageDownloader.h"  
    2.   
    3. @implementation ImageDownloader  
    4.   
    5. @synthesize data=_data;  
    6. @synthesize delegate;  
    7. @synthesize delPara;  
    8.   
    9. - (void)dealloc   
    10. {  
    11.       
    12.     [_request release];   
    13.     _request=nil;  
    14.       
    15.     [_data release];  
    16.     _data=nil;  
    17.       
    18.     [_connection release];  
    19.     _connection=nil;  
    20.       
    21.     [delPara release];  
    22.       
    23.     [super dealloc];  
    24.       
    25. }  
    26. - (id)initWithURLString:(NSString *)url   
    27. {  
    28.       
    29.     self = [self init];  
    30.     if (self) {  
    31.   
    32.         _request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]];  
    33.           
    34.         _data = [[NSMutableData data] retain];  
    35.           
    36.     }  
    37.       
    38.     return self;  
    39.       
    40. }  
    41.   
    42.   
    43.   
    44. // 开始处理-本类的主方法  
    45.   
    46. - (void)start {  
    47.       
    48.     if (![self isCancelled]) {  
    49.           
    50.         [NSThread sleepForTimeInterval:3];  
    51.         // 以异步方式处理事件,并设置代理  
    52.           
    53.         _connection=[[NSURLConnection connectionWithRequest:_request delegate:self]retain];  
    54.           
    55.         while(_connection != nil) {  
    56.               
    57.             [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];     
    58.               
    59.         }  
    60.           
    61.     }  
    62.       
    63. }  
    64.   
    65. #pragma mark NSURLConnection delegate Method  
    66.   
    67. // 接收到数据(增量)时  
    68.   
    69. - (void)connection:(NSURLConnection*)connection  
    70.   
    71.     didReceiveData:(NSData*)data   
    72. {  
    73.     // 添加数据  
    74.     [_data appendData:data];  
    75.       
    76. }  
    77.   
    78. - (void)connectionDidFinishLoading:(NSURLConnection*)connection {  
    79.       
    80.     [_connection release],  
    81.     _connection=nil;  
    82.     NSLog(@"%s", __func__);  
    83.       
    84.     UIImage *img = [[[UIImage alloc] initWithData:self.data] autorelease];  
    85.       
    86.     if (self.delegate != nil)  
    87.     {  
    88.         [delegate imageDidFinished:img para:self.delPara];  
    89.     }  
    90.       
    91.       
    92. }  
    93.   
    94. -(void)connection: (NSURLConnection *) connection didFailWithError: (NSError *) error  
    95. {  
    96.     [_connection release],  
    97.     _connection=nil;   
    98. }  
    99.   
    100. -(BOOL)isConcurrent   
    101. {  
    102.     //返回yes表示支持异步调用,否则为支持同步调用  
    103.     return YES;  
    104.       
    105. }  
    106. - (BOOL)isExecuting  
    107. {  
    108.     return _connection == nil;   
    109. }  
    110. - (BOOL)isFinished  
    111. {  
    112.     return _connection == nil;    
    113. }  


    应用举例:

           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;//附带的参数;
    }




  • 相关阅读:
    Python容器篇 4 -- 字典
    Python容器篇 3 -- 元组
    Python容器篇 2 -- 列表
    Python容器篇 1 -- 字符串
    Python中的关键字
    SQLI-LABS靶场环境搭建详细流程
    Qt QLineEdit 改变text内容的大小
    linux下QT连接mysql找不到驱动
    apt(rpm) Mysql安装
    const 成员函数
  • 原文地址:https://www.cnblogs.com/jackljf/p/3589301.html
Copyright © 2011-2022 走看看