#import "ViewController.h"
@interface Viewcontroller () <nsurlconnectiondatadelegate>
@property (weak, nonatomic) iboutlet uiprogressview *progressview;
@property (nonatomic, assign) nsinteger totalsize;
@property (nonatomic, assign) nsinteger currentsize;
/** Sandbox Path * /
@property (nonatomic, strong) nsstring *fullpath;
/** Connection Object * /
@property (nonatomic, strong) nsurlconnection *connect;
/** Output Stream * /
@property (nonatomic, strong) nsoutputstream *stream;
@end
@implementation Viewcontroller
-(ibaction) Startbtnclick: (ID) Sender {
[self download];
}
-(ibaction) Cancelbtnclick: (ID) Sender {
[self. Connect cancel];
}
-(ibaction) Goonbtnclick: (ID) Sender {
[self download];
}
Memory Spike
-(void) Download
{
//1.url
nsurl *url = [nsurl urlwithstring:@ "http://www.33lc.com/article/UploadPic/2012-10/ 2012102514201759594.jpg "];
//2. Create Request Object
nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl: url];
// Set the request header information to tell the server value to request part of the data range
/*
bytes=0-100
bytes=-100
bytes=0- all data after the request
*/
nsstring *range = [nsstring stringwithformat:@ "bytes=%zd-",self. CurrentSize];
[Request SetValue: Range Forhttpheaderfield:@ "range"];
NSLog(@ "+++++++%@", Range);
//3. Send Request
nsurlconnection *connect = [[nsurlconnection alloc]initwithrequest: Request delegate:self];
self. Connect = connect;
}
#pragma Mark----------------------
#pragma Mark Nsurlconnectiondatadelegate
-(void) connection: (nsurlconnection *) connection didreceiveresponse: (nsurlresponse * ) Response
{
NSLog(@ "Didreceiveresponse");
//1. Gets the total size of the file (the total size of the file data for this request!! = file )
//Self.totalsize = Response.expectedcontentlength + self.currentsize;
if (self. CurrentSize >0) {
return;
}
self. TotalSize = response. Expectedcontentlength;
//2. write the data into the sandbox
self. FullPath = [[nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask , YES) lastobject]stringbyappendingpathcomponent:@ "123.jpg"];
NSLog(@ "%@",self. FullPath);
//3. Create an output stream
Nsoutputstream
Nsinputstream
/*
first parameter : the path to a file
second parameter : YES append
feature : if the output stream points to an address that does not have a file, an empty file is created automatically
*/
nsoutputstream *stream = [[nsoutputstream alloc]inittofileatpath:Self . fullPath append:YES];
// open output stream
[Stream open];
self. Stream = stream;
}
-(void) connection: (nsurlconnection *) connection didreceivedata: (nsdata *) Data
{
// Write Data
[self. Stream write:d ata. Bytes maxLength:d ata. Length];
//3. Get Progress
self. CurrentSize + = data. Length;
// Progress = Total size of downloaded/file
NSLog(@ "%f",1.0 * self. CurrentSize/self. TotalSize);
self. Progressview. Progress = 1.0 * self. CurrentSize/self. TotalSize;
//nslog (@ "%@", Self.fullpath);
}
-(void) connection: (nsurlconnection *) connection didfailwitherror: (nserror *) Error
{
}
-(void) connectiondidfinishloading: (nsurlconnection *) connection
{
// close stream
[self. Stream close];
self. Stream = nil;
NSLog(@ "connectiondidfinishloading");
NSLog(@ "%@",self. FullPath);
}
@end
Nsurlconnection Large File Download