ios block dispatch_queue_t 使用demo.
2012-08-17 18:03
561 查看
关于block的介绍我就不多说了.
dispatch_queue_t 还在学习了解当中..
2者结合的好处就是异步处理数据.在方法内部返回结果,直接可以直接使用方法类的变量,类的成员和变量等..优化减少代码量.
使用的IDE是xcode4.5测试版.
代码使用ARC模式.ios6下面dispatch_queue_release已经取消.低于ios6版本的SDK在CWImageInfo的dealloc内添加.
新建 CWImageInfo类.
实现代码:
修改启动入口:
ViewController.m内添加
dispatch_queue_t 还在学习了解当中..
2者结合的好处就是异步处理数据.在方法内部返回结果,直接可以直接使用方法类的变量,类的成员和变量等..优化减少代码量.
使用的IDE是xcode4.5测试版.
代码使用ARC模式.ios6下面dispatch_queue_release已经取消.低于ios6版本的SDK在CWImageInfo的dealloc内添加.
dispatch_queue_release(_queue);
新建 CWImageInfo类.
#import <Foundation/Foundation.h> @interface CWImageInfo : NSObject { //block void (^_block_) (UIImage *image, NSUInteger nTag, NSString *sKey); dispatch_queue_t _queue_; } - (BOOL) downloadWithURL:(NSURL *) url tag:(NSUInteger) nTag key:(NSString *) sKey; #if NS_BLOCKS_AVAILABLE - (void) setCompleteBlock:(void (^)(UIImage *image, NSUInteger nTag, NSString *sKey))block; - (BOOL) downloadWithURL:(NSURL *) url tag:(NSUInteger) nTag key:(NSString *) sKey block:(void (^)(UIImage *image, NSUInteger nTag, NSString *sKey))block; #endif @end
实现代码:
#import "CWImageInfo.h" @implementation CWImageInfo - (id) init { if (self = [super init]) { if (_queue_ == nil) { _queue_ = dispatch_queue_create("com.watsy.imageDownload", nil); } } return self; } - (BOOL) downloadWithURL:(NSURL *) url tag:(NSUInteger) nTag key:(NSString *) sKey { if (url == nil) { return NO; } dispatch_async(_queue_, ^{ NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *image = [UIImage imageWithData:data]; dispatch_async(dispatch_get_main_queue(), ^{ _block_(image, nTag, sKey); }); }); return YES; } - (BOOL) downloadWithURL:(NSURL *) url tag:(NSUInteger) nTag key:(NSString *) sKey block:(void (^)(UIImage *image, NSUInteger nTag, NSString *sKey))block { if (url == nil) { return NO; } dispatch_async(_queue_, ^{ NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *image = [UIImage imageWithData:data]; dispatch_async(dispatch_get_main_queue(), ^{ block(image, nTag, sKey); }); }); return YES; } - (void) setCompleteBlock:(void (^) (UIImage *image, NSUInteger nTag, NSString *sKey)) block { _block_ = block; } @end
修改启动入口:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; self.window.rootViewController = navController; [self.window makeKeyAndVisible]; return YES; }
ViewController.m内添加
- (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; UIActivityIndicatorView *IndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)]; IndicatorView.backgroundColor = [UIColor clearColor]; IndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite; UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithCustomView:IndicatorView]; self.navigationItem.rightBarButtonItem = refreshItem; [IndicatorView startAnimating]; NSArray *imageAddArray = [NSArray arrayWithObjects: @"http://www.xuefo.net/user/editor/uploadfile/20111128175139918.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175139846.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175140661.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175140907.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175143708.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175143551.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175144636.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175144761.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175147655.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175147652.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175148276.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175148942.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175148505.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175148894.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175149393.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175149643.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175149315.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175149619.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175150388.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175150462.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175150902.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175150699.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175151668.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175151565.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175151818.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175151769.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175152107.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175152489.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175152906.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175152111.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175153957.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175153205.jpg", @"http://www.xuefo.net/user/editor/uploadfile/20111128175153252.jpg", @"",nil]; NSMutableArray *imageArray = [[NSMutableArray alloc] initWithCapacity:10]; CWImageInfo *cImgInfo = [[CWImageInfo alloc] init]; for (int i = 0; i < [imageAddArray count]; i++) { NSURL *url = [NSURL URLWithString:[imageAddArray objectAtIndex:i]]; [cImgInfo downloadWithURL:url tag:i key:[url path] block:^(UIImage *image, NSUInteger nIndex, NSString *sKey){ NSLog(@"download index:%d key:%@",nIndex,sKey); if (image != nil) { [imageArray addObject:image]; } //全部下载完毕 if(nIndex == [imageAddArray count] - 1) { self.navigationItem.rightBarButtonItem = nil; self.ibAnimationImageView.animationImages = imageArray; self.ibAnimationImageView.animationDuration = nIndex; [self.ibAnimationImageView startAnimating]; } }]; } }
相关文章推荐
- IOS block dispatch_queue 请求 接受数据 block 接口
- iOS开发之:dispatch_async 与 dispatch_get_global_queue 的使用方法
- IOS block dispatch_queue
- iOS开发之:dispatch_async 与 dispatch_get_global_queue 的使用方法
- iOS开发之:dispatch_async 与 dispatch_get_global_queue 的使用方法
- 关于iOS Block使用的demo
- IOS-12-关于GCD多线程:dispatch_async 与 dispatch_get_global_queue 的使用方法
- iOS开发之:dispatch_async 与 dispatch_get_global_queue 的使用方法
- iOS UI异步更新:dispatch_async 与 dispatch_get_global_queue 的使用方法
- IOS-dispatch_async 、dispatch_sync与 dispatch_get_global_queue 的使用方法
- iOS开发之:dispatch_async 与 dispatch_get_global_queue 的使用方法
- iOS开发之:dispatch_async 与 dispatch_get_global_queue 的使用方法
- iOS 多线程下NSOperation、NSBlockOperation、NSInvocationOperation、NSOperationQueue的使用
- IOS block dispatch_queue 请求 接受数据 block 接口
- iOS开发之:dispatch_async 与 dispatch_get_global_queue 的使用方法
- iOS开发之:dispatch_async 与 dispatch_get_global_queue 的使用方法
- IOS 异步获取数据并刷新界面dispatch_async的使用方法
- ios - block循环引用Demo示例
- IOS-20-数据库操作之(SQLite第三方封装库FMDB)的基础入门使用及demo源码
- IOS多线程编程之NSOperation和NSOperationQueue的使用