设置下载NSData的进度条

时间:2013-11-07 09:14:03

标签: ios xcode4.5

NSURL *url = [NSURL URLWithString:@"http://i0.kym-cdn.com/entries/icons/original/000/005/545/OpoQQ.jpg?1302279173"]; 
NSData *data = [NSData dataWithContentsOfURL:url]; 
imageView.image = [[[UIImage imageWithData:data];

我想在下载时设置进度条。

3 个答案:

答案 0 :(得分:12)

举一个更详细的例子:

你的.h文件中的

@interface YourClass : YourSuperclass<NSURLConnectionDataDelegate>
你的.m文件中的

@property (nonatomic) NSMutableData *imageData;
@property (nonatomic) NSUInteger totalBytes;
@property (nonatomic) NSUInteger receivedBytes;

在某个地方打电话

NSURL *url = [NSURL URLWithString:@"http://i0.kym-cdn.com/entries/icons/original/000/005/545/OpoQQ.jpg?1302279173"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

并实现委托方法

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) urlResponse;
    NSDictionary *dict = httpResponse.allHeaderFields;
    NSString *lengthString = [dict valueForKey:@"Content-Length"];
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    NSNumber *length = [formatter numberFromString:lengthString];
    self.totalBytes = length.unsignedIntegerValue;

    self.imageData = [[NSMutableData alloc] initWithCapacity:self.totalBytes];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.imageData appendData:data];
    self.receivedBytes += data.length;

    // Actual progress is self.receivedBytes / self.totalBytes
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    imageView.image = [UIImage imageWithData:self.imageData];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    //handle error
}

答案 1 :(得分:2)

使用该方法无法获得进度回调。

您需要使用NSURLConnectionNSURLConnectionDataDelegate

然后NSURLConnection以异步方式运行,并将回调发送给其委托。

要看的主要是......

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;

- (void)connectionDidFinishLoading:(NSURLConnection *)connection;

这些都用于获取你正在做的事情的连接。

修改

实际上,请参阅下面Marc的回答。这是对的。

答案 2 :(得分:-2)

您可以使用MBProgress Hud类加载视图。您只能从此处下载两个课程: - https://github.com/jdg/MBProgressHUD 在该类中编写要加载数据的代码之后 示例:在viewDidLoad中编写此

- (void) viewDidLoad
{
    MBProgressHud *spinner =  [MBProgressHUD showHUDAddedTo:self.view animated:YES];

        spinner.mode = MBProgressHUDModeCustomView;

        [spinner setLabelText:@"Loading....."];

        [spinner setLabelFont:[UIFont systemFontOfSize:15]];

        [spinner show:YES];

        [self performSelectorInBackground:@selector(getData) withObject:nil];
}

- (void) getData
{
     NSURL *url = [NSURL URLWithString:@"http://i0.kym-cdn.com/entries/icons/original/000/005/545/OpoQQ.jpg?1302279173"]; 

        NSData *data = [NSData dataWithContentsOfURL:url]; 

        imageView.image = [[[UIImage imageWithData:data];

        [spinner hide:YES];

        [spinner removeFromSuperViewOnHide];
}