AFNetworking斷點續傳、下載圖片

來源:互聯網
上載者:User

標籤:斷點續傳   下載圖片   

下載圖片:下載圖片後儲存在本地檔案中

//在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斷點續傳、下載圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.