標籤:斷點續傳 下載圖片
下載圖片:下載圖片後儲存在本地檔案中
//在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斷點續傳、下載圖片