Set progress bar for downloading NSData

好久不见. 提交于 2019-12-03 08:19:00

To give a more detailed example:

in your .h file do

@interface YourClass : YourSuperclass<NSURLConnectionDataDelegate>

in your .m file do

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

And somewhere call

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];

And also implement the delegate methods

- (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
}

You can't get progress call backs by using that method.

You need to use an NSURLConnection and NSURLConnectionDataDelegate.

The NSURLConnection then runs asynchronously and will send callbacks to its delegate.

The main ones to look at are...

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

and

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

These are all used for getting the connection to do what you're already doing.

EDIT

Actually, see Marc's answer below. It is correct.

You can use MBProgress Hud class for loading view. You can download only two classes from here :-https://github.com/jdg/MBProgressHUD After you write this code in that class which you want to load the data Example :In your viewDidLoad you write this

- (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];
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!