码迷,mamicode.com
首页 > Web开发 > 详细

AFNetworking断点续传、下载图片

时间:2015-02-27 17:08:25      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:断点续传   下载图片   

下载图片:下载图片后保存在本地文件中

//在lock和挂起状态都会下载  
NSURLRequest *request=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://attach.bbs.miui.com/forum/201110/05/082552rm0v11zlv1axd13l.png"]];  
//操作配置  
AFHTTPRequestOperation *requestOperator=[[AFHTTPRequestOperation alloc]initWithRequest:request];  
requestOperator.responseSerializer=[AFImageResponseSerializer serializer];  
[requestOperator setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){  
    //主线程中执行  
    self.pictureView.image=responseObject;  
    NSString *fileDestination=[NSHomeDirectory() stringByAppendingString:@"/Documents/1.png"];  
    <span style="color:#ff6666;">//若respondObject为NSData,则用下面的方法</span>  
    /*NSFileManager *fileManage=[NSFileManager defaultManager]; 
    if([fileManage createFileAtPath:fileDestination contents:imgData attributes:nil]){ 
        NSLog(@"%@",fileDestination); 
    }*/  
    if([UIImageJPEGRepresentation(responseObject, 1.0) writeToFile:fileDestination atomically:YES]){  
        //成功后运行  
        NSLog(@"%@",fileDestination);  
    }  
      
} failure:^(AFHTTPRequestOperation *operation, NSError *error){  
    NSLog(@"error:%@",error);  
}];  
//监听下载进度  
[requestOperator setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead){  
    self.progressView.progress=totalBytesRead*1.0/totalBytesExpectedToRead;  
    //NSLog(@"%.3f",totalBytesRead*1.0/totalBytesExpectedToRead);  
}];  
//开始执行  
[requestOperator start];  



断点续传:暂停和开始下载的按钮我都是一个按钮,所以定义了AFHTTPRequestOperation *resumeOperation和一个BOOL类型的resume判断是否续传。

因为在block中又调用了self,会造成内存泄露,所以定义了__weak的weakSelf解决内存泄露的问题


<span style="line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; margin: 0px; padding: 0px; border: none; background-color: inherit;"><span style=</span><span class="string" style="line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"color:#ff0000;"</span><span style="line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; margin: 0px; padding: 0px; border: none; background-color: inherit;">>__weak </span><span class="keyword" style="line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">ViewController</span><span style="line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; margin: 0px; padding: 0px; border: none; background-color: inherit;"> *weakSelf=</span><span class="keyword" style="line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">self</span><span style="line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; margin: 0px; padding: 0px; border: none; background-color: inherit;">;</span>  </span>
if(!self.resumeOperation){  
        NSURLRequest *request=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://attach.bbs.miui.com/forum/201110/05/082552rm0v11zlv1axd13l.png"]];  
        self.resumeOperation=[[AFHTTPRequestOperation alloc]initWithRequest:request];  
        self.resumeOperation.responseSerializer=[AFImageResponseSerializer serializer];  
        [weakSelf.resumeOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){  
            //主线程中执行  
            self.pictureView.image=responseObject;  
            //保存文件  
            NSString *fileDestination=[NSHomeDirectory() stringByAppendingString:@"/Documents/1.png"];  
            if([UIImageJPEGRepresentation(responseObject, 1.0) writeToFile:fileDestination atomically:YES]){  
                NSLog(@"%@",fileDestination);  
            }  
            self.resumeOperation=nil;  
              
        } failure:^(AFHTTPRequestOperation *operation, NSError *error){  
            self.resumeOperation=nil;  
            NSLog(@"error:%@",error);  
        }];  
        //监听下载进度  
        [weakSelf.resumeOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead){  
            self.progressView.progress=totalBytesRead*1.0/totalBytesExpectedToRead;  
            //NSLog(@"%.3f",totalBytesRead*1.0/totalBytesExpectedToRead);  
        }];  
        //开始执行  
        [self.resumeOperation start];  
    }else if(!self.resume){  
        [self.resumeOperation pause];  
        self.resume=YES;  
    }else{  
        //继续  
        [self.resumeOperation resume];  
        self.resume=NO;  
    }  




AFNetworking断点续传、下载图片

标签:断点续传   下载图片   

原文地址:http://blog.csdn.net/u013247242/article/details/43968741

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!