关于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];
}
}];
}
}
本文介绍了一个使用Block和GCD实现的iOS图片异步加载类CWImageInfo。该类能够从网络下载图片,并在主线程中更新UI,适用于大量图片的预加载场景。
899

被折叠的 条评论
为什么被折叠?



