NSURLConnectionDataDelegate和NSURLConnectionDownloadDelegate实现两者都无法正常工作

时间:2014-02-13 08:02:02

标签: ios iphone objective-c nsurlconnection

我想实现两个委托。虽然实现了两个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..");
}

0 个答案:

没有答案