Afnetworking breakpoint resume, download picture

Source: Internet
Author: User
Tags save file

Download Image : Saved in a local file after downloading pictures

Both the lock and suspend states are downloaded Nsurlrequest *request=[[nsurlrequest alloc]initwithurl:[nsurl urlwithstring:@ "/http/  Attach.bbs.miui.com/forum/201110/05/082552rm0v11zlv1axd13l.png "];  Operation configuration Afhttprequestoperation *requestoperator=[[afhttprequestoperation Alloc]initwithrequest:request];  Requestoperator.responseserializer=[afimageresponseserializer Serializer]; [Requestoperator setcompletionblockwithsuccess:^ (afhttprequestoperation *operation, id responseObject)      {//The main thread executes self.pictureview.image=responseobject;      NSString *filedestination=[nshomedirectory () stringbyappendingstring:@ "/documents/1.png"]; <span style= "color: #ff6666;"     >//if Respondobject is NSData, then use the following method </span>/*nsfilemanager *filemanage=[nsfilemanager Defaultmanager];     if ([Filemanage createfileatpath:filedestination contents:imgdata Attributes:nil]) {NSLog (@ "%@", filedestination);     }*/if ([UIImageJPEGRepresentation (Responseobject, 1.0) writetofile:filedestination Atomically:yes]) {     After successful run NSLog (@ "%@", filedestination);  }} failure:^ (Afhttprequestoperation *operation, Nserror *error) {NSLog (@ "error:%@", error);  }]; Listen for download progress [Requestoperator setdownloadprogressblock:^ (Nsuinteger bytesread, Long long totalbytesread, long long      Totalbytesexpectedtoread) {self.progressview.progress=totalbytesread*1.0/totalbytesexpectedtoread;  NSLog (@ "%.3f", Totalbytesread*1.0/totalbytesexpectedtoread);  }];   Start execution [requestoperator start];



Breakpoint Continuation: pause and start the download button I am a button, so I define the afhttprequestoperation * Resumeoperation and a bool type of resume are judged for continuation.

Because self is called in the block, memory leaks are caused, so the __weak weakself is defined to solve the memory leak problem


/span>

<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://a          Ttach.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)              {//The main thread executes self.pictureview.image=responseobject;              Save file 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);          }]; Listen for download progress [weakself.resumeoperation setdownloadprogressblock:^ (Nsuinteger bytesread, Long long Totalbytesread, Lon              G long Totalbytesexpectedtoread) {self.progressview.progress=totalbytesread*1.0/totalbytesexpectedtoread;          NSLog (@ "%.3f", Totalbytesread*1.0/totalbytesexpectedtoread);          }];      Start execution [self.resumeoperation start];          }else if (!self.resume) {[self.resumeoperation pause];      Self.resume=yes;          }else{//Continue [self.resumeoperation resume];      Self.resume=no;   }




Afnetworking breakpoint resume, download picture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.