IOS 多线程下载小试牛刀 GCD

本文介绍了一种使用异步方式加载按钮图片的方法,并通过代码示例展示了如何利用 GCD 和缓存来提高加载效率。


本人一向不喜欢废话,直接上代码

异步加载按钮图片 。。。。。  


-(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);  
    
}







评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值