iOS基于SDWebimage和urlConnection简单的带进度条图片加载

第一次写csdn 请多多关照


注:

objc/runtime.h 

Objecitve-C的重要特性是Runtime(运行时),在Interacting with the Runtime(交互运行)中,运行时函数部分,苹果给出了/usr/lib/libobjc.A.dylib库,这个共享库提供支持动态属性的objective - c语言,通过其接口,可以用于开发将其他语言运行于Objective-C上的中间层(桥接层),库里的函数定义为纯C语言。



UIImageView+ZCDowndLoad

头文件 : UIImageView+ZCDownload.h

#import <UIKit/UIKit.h>
//进度条imageView
typedef void  (^ZCDownloadCompletedBlock)(UIImage *image, NSError *error);

@interface UIImageView (ZCDownload)
/*
 *异步下载图片带进度条
 *url 图片下载地址
 *completedBlock 下载完成调用的block
 */
- (void)zcSetImageAnimationWithURL:(NSURL *)url completed:(ZCDownloadCompletedBlock)completedBlock;
@end</span>

.m文件  UIImageView+ZCDownload.m


#import "UIImageView+ZCDownload.h"
#import <SDWebImage/SDImageCache.h>
#import <objc/runtime.h>
@interface  UIImageView()<NSURLConnectionDataDelegate>

@property (nonatomic,strong)    NSMutableData                   *downloadData;
@property(nonatomic,assign)     long long                       sumLength;
@property(nonatomic,strong)     ZCDownloadCompletedBlock        block;
@property(nonatomic,copy)       NSString                        *urlString;
@end

@implementation UIImageView (ZCDownload)
static char urlStringKey;
static char blockKey;
static char sumLengthKey;
//static char currentLengthKey;
static char downloadDataKey;


- (void)zcSetImageAnimationWithURL:(NSURL *)url completed:(ZCDownloadCompletedBlock)completedBlock{
    self.block=completedBlock;
    self.urlString=url.absoluteString;
    self.image=[UIImage imageNamed:@"Load001"];
    if (self.downloadData==nil) {
        self.downloadData=[NSMutableData data];
    }
    
    UIImage *image=[[SDImageCache sharedImageCache] imageFromDiskCacheForKey:self.urlString];
    if (image==nil) {
        NSURLRequest *request=[NSURLRequest requestWithURL:url];
        
        [[NSURLConnection connectionWithRequest:request delegate:self] start];

    }else
    {
        self.image=image;
        self.block(image,nil);
    }
}
#pragma mark    - get & set
-(NSString *)urlString{
    return objc_getAssociatedObject(self, &urlStringKey);
}
-(void)setUrlString:(NSString *)urlString{
    objc_setAssociatedObject(self, &urlStringKey,
                             urlString,
                             OBJC_ASSOCIATION_COPY_NONATOMIC);
}
-(ZCDownloadCompletedBlock)block{
    return objc_getAssociatedObject(self, &blockKey);
}
-(void)setBlock:(ZCDownloadCompletedBlock)block{
    objc_setAssociatedObject(self, &blockKey,
                             block,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(long long)sumLength{
    return [(NSNumber *)objc_getAssociatedObject(self, &sumLengthKey) longLongValue];
    
}
-(void)setSumLength:(long long)sumLength{
    objc_setAssociatedObject(self, &sumLengthKey,
                             [NSNumber numberWithLongLong:sumLength],
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(NSMutableData *)downloadData{
 return objc_getAssociatedObject(self, &downloadDataKey);
}
-(void)setDownloadData:(NSMutableData *)downloadData{
    objc_setAssociatedObject(self, &downloadDataKey,
                            downloadData,
                             OBJC_ASSOCIATION_RETAIN);
}
#pragma mark    - url ConnextionDelegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    if(self.sumLength==0){
        self.sumLength=response.expectedContentLength;
    }
#if DEBUG
    NSLog(@"%lld",response.expectedContentLength);
#endif
//    [self.downloadData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

#if DEBUG
    NSLog(@"%d",(int)self.downloadData.length);
    NSLog(@"%lld",self.sumLength);

#endif
    
    [self.downloadData appendData:data];
    double progress=(double)self.downloadData.length/self.sumLength;
//    NSLog(@"%@",[NSString stringWithFormat:@"Load00%d",(int)((double)(progress*19+1))]);
    self.image=[UIImage imageNamed:[NSString stringWithFormat:@"Load00%d",(int)((double)(progress*19+1))]];
    
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
#if DEBUG
 
    NSLog(@"%lld",self.sumLength);
#endif
    [[SDImageCache sharedImageCache] storeImage:[[UIImage alloc]initWithData:self.downloadData] forKey:self.urlString];
    
    self.image=[[UIImage alloc]initWithData:self.downloadData];
     self.block(self.image,nil);
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy  timeoutInterval:10.f];
 
//    [request setValue:[NSString stringWithFormat:@"bytes %d-%d/%d",(int)self.downloadData.length,(int)self.sumLength,(int)self.sumLength] forHTTPHeaderField:@"Content-Range"];
//    [request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
    [request addValue:[NSString stringWithFormat:@"bytes=%llu-",(unsigned long long)self.downloadData.length] forHTTPHeaderField:@"Range"];

    [[NSURLConnection connectionWithRequest:request delegate:self] start];
    

    
}
@end</span>

UIButton+ZCDownload同理









  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值