本人一向不喜欢废话,直接上代码
异步加载按钮图片 。。。。。
-(void)downLoadRemainPersonLogoImage{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
EGOCache *egoCach=[EGOCache globalCache];
NSInteger logoImgCount =[logoUrlList count];
dispatch_group_async(group, queue, ^{
UIImage *img0=[egoCach imageForKey:[logoUrlList[0] lastPathComponent]];
if (img0 == nil) {
NSData *d0=[NSData dataWithContentsOfURL:[NSURL URLWithString:logoUrlList[0]]];
img0=[UIImage imageWithData:d0];
[egoCach setImage:img0 forKey:[logoUrlList[0] lastPathComponent]];
}
NSDictionary *data=remainPersonList[0];
[unselectPeopleLogosList replaceObjectAtIndex:0 withObject:[self unselectImage:[data[@"remindTime"] substringWithRange:NSMakeRange(11, 5)] logImg:img0]];
[selectPeopleLogosList replaceObjectAtIndex:0 withObject:[self selectImage:[data[@"remindTime"] substringWithRange:NSMakeRange(11, 5)] logImg:img0]];
});
dispatch_group_async(group, queue, ^{
UIImage *img1=[egoCach imageForKey:[logoUrlList[1] lastPathComponent]];
if (img1 == nil) {
NSData *d1=[NSData dataWithContentsOfURL:[NSURL URLWithString:logoUrlList[1]]];
img1=[UIImage imageWithData:d1];
[egoCach setImage:img1 forKey:[logoUrlList[1] lastPathComponent]];
}
NSDictionary *data=remainPersonList[1];
[unselectPeopleLogosList replaceObjectAtIndex:1 withObject:[self unselectImage:[data[@"remindTime"] substringWithRange:NSMakeRange(11, 5)] logImg:img1]];
[selectPeopleLogosList replaceObjectAtIndex:1 withObject:[self selectImage:[data[@"remindTime"] substringWithRange:NSMakeRange(11, 5)] logImg:img1]];
});
dispatch_group_async(group, queue, ^{
UIImage *img2=[egoCach imageForKey:[logoUrlList[2] lastPathComponent]];
if (img2 == nil) {
NSData *d2=[NSData dataWithContentsOfURL:[NSURL URLWithString:logoUrlList[2]]];
img2=[UIImage imageWithData:d2];
[egoCach setImage:img2 forKey:[logoUrlList[2] lastPathComponent]];
}
NSDictionary *data=remainPersonList[2];
[unselectPeopleLogosList replaceObjectAtIndex:2 withObject:[self unselectImage:[data[@"remindTime"] substringWithRange:NSMakeRange(11, 5)] logImg:img2]];
[selectPeopleLogosList replaceObjectAtIndex:2 withObject:[self selectImage:[data[@"remindTime"] substringWithRange:NSMakeRange(11, 5)] logImg:img2]];
});
dispatch_group_async(group, queue, ^{
UIImage *img3=[egoCach imageForKey:[logoUrlList[3] lastPathComponent]];
if (img3 == nil) {
NSData *d3=[NSData dataWithContentsOfURL:[NSURL URLWithString:logoUrlList[3]]];
img3=[UIImage imageWithData:d3];
[egoCach setImage:img3 forKey:[logoUrlList[3] lastPathComponent]];
}
NSDictionary *data=remainPersonList[3];
[unselectPeopleLogosList replaceObjectAtIndex:3 withObject:[self unselectImage:[data[@"remindTime"] substringWithRange:NSMakeRange(11, 5)] logImg:img3]];
[selectPeopleLogosList replaceObjectAtIndex:3 withObject:[self selectImage:[data[@"remindTime"] substringWithRange:NSMakeRange(11, 5)] logImg:img3]];
});
dispatch_group_async(group, queue, ^{
UIImage *img4=[egoCach imageForKey:[logoUrlList[4] lastPathComponent]];
if (img4 == nil) {
NSData *d4=[NSData dataWithContentsOfURL:[NSURL URLWithString:logoUrlList[4]]];
img4=[UIImage imageWithData:d4];
[egoCach setImage:img4 forKey:[logoUrlList[4] lastPathComponent]];
}
NSDictionary *data=remainPersonList[4];
[unselectPeopleLogosList replaceObjectAtIndex:4 withObject:[self unselectImage:[data[@"remindTime"] substringWithRange:NSMakeRange(11, 5)] logImg:img4]];
[selectPeopleLogosList replaceObjectAtIndex:4 withObject:[self selectImage:[data[@"remindTime"] substringWithRange:NSMakeRange(11, 5)] logImg:img4]];
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
///// 上面的步骤完成之后的操作。。。。。。
for (int i=0; i<[logoButtonList count]; i++) {
if (i==lastButtonIndex) {
UIButton *b1=logoButtonList[i];
id im=selectPeopleLogosList[i];
UIImage *i=(UIImage *)im;
[b1 setImage:i forState:UIControlStateNormal];
[b1 setImage:i forState:UIControlStateHighlighted];
}else{
UIButton *b1=logoButtonList[i];
id im=unselectPeopleLogosList[i];
UIImage *i=(UIImage *)im;
[b1 setImage:i forState:UIControlStateNormal];
[b1 setImage:i forState:UIControlStateHighlighted];
}
}
});
dispatch_release(group);
}
本文介绍了一种使用异步方式加载按钮图片的方法,并通过代码示例展示了如何利用 GCD 和缓存来提高加载效率。

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



