我想实现两个委托。虽然实现了两个NSURLConnectionDataDelegate
方法的实现。如果我删除NSURLConnectionDownloadDelegate
方法,则NSURLConnectionDataDelegate
方法正常运行。我没理由。以下是代码。
-(void)downloadImageFromServer
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http:/*************/-yqaDz5cGzAY/USm27B4zbOI/AAAAAAAADEA/3sgkCfGNwqw/s1600/lion_hd_wallpaper.jpg"]];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.mData = [NSMutableData data];
NSLog(@"http header fields %@", [(NSHTTPURLResponse*)response allHeaderFields]);
NSDictionary *headerFields = [(NSHTTPURLResponse*)response allHeaderFields];
long int dataLength = [[headerFields valueForKey:@"Content-Length"] intValue];
self.progress = [NSProgress progressWithTotalUnitCount:dataLength];
[_progress addObserver:self
forKeyPath:@"fractionCompleted"
options:NSKeyValueObservingOptionNew
context:NULL];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_mData appendData:data];
self.ImageViewer.image = [UIImage imageWithData:_mData];
}
- (void)connection:(NSURLConnection *)connection didWriteData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long) expectedTotalBytes
{
NSLog(@"DOwnloading..");
}