ios block dispatch_queue_t 使用demo.
来源:互联网 发布:mysql 删除表结构 编辑:程序博客网 时间:2024/06/15 07:22
关于block的介绍我就不多说了.
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;}
- (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_t 使用demo.
- 关于iOS Block使用的demo
- iOS Block传值~demo
- IOS-线程异步操作dispatch_queue_t
- ios 中使用 block
- IOS 使用Block
- iOS 使用Block
- IOS之Block使用
- iOS block使用
- iOS学习-Block使用
- ios block初级使用
- iOS block的使用
- ios block 的使用
- iOS Block使用
- iOS Block使用
- iOS block使用总结
- iOS Block的使用
- iOS~block的使用
- Xp,win7下 jar文件双击不能运行,或无法发现main方法,java -jar的问题解决
- java.io.PushbackReader
- test018
- 熊向阳:PR更新以及提高PR的必要因素
- Linux创建普通用户和管理员用户
- ios block dispatch_queue_t 使用demo.
- ext-4.0.7-gpl 目录说明
- ireport 自动换行
- 打开office2007弹出“The setup controller has encountered
- 启用IIS的GZIP压缩功能的图文方法
- 索引的分类,从唯一性索引(unique)拓展来说
- BS与CS的联系与区别
- BI入门经典 (转)
- 数据完整性