Définir la barre de progression pour download NSData

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

Je veux définir la barre de progression pendant le téléchargement.

Pour donner un exemple plus détaillé:

dans votre file .h faire

 @interface YourClass : YourSuperclass<NSURLConnectionDataDelegate> 

dans votre file .m faire

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

Et quelque part appel

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

Et également mettre en œuvre les methods de délégué

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) urlResponse; NSDictionary *dict = httpResponse.allHeaderFields; NSSsortingng *lengthSsortingng = [dict valueForKey:@"Content-Length"]; NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; NSNumber *length = [formatter numberFromSsortingng:lengthSsortingng]; 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 } 

Vous ne pouvez pas get de callbacks de progression en utilisant cette méthode.

Vous devez utiliser un NSURLConnection et NSURLConnectionDataDelegate .

Le NSURLConnection s'exécute alors de manière asynchronous et enverra des callbacks à son délégué.

Les principaux à regarder sont …

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

et

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

Ils sont tous utilisés pour get la connection pour faire ce que vous faites déjà.

MODIFIER

En fait, voir la réponse de Marc ci-dessous. C'est correct.

Vous pouvez utiliser la class MBProgress Hud pour charger la vue. Vous pouvez download seulement deux classs à partir d'ici: – https://github.com/jdg/MBProgressHUD Après avoir écrit ce code dans la class que vous souhaitez charger datatables Exemple: Dans votre viewDidLoad vous écrivez ceci

 - (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 URLWithSsortingng:@"http://i0.kym-cdn.com/ensortinges/icons/original/000/005/545/OpoQQ.jpg?1302279173"]; NSData *data = [NSData dataWithContentsOfURL:url]; imageView.image = [[[UIImage imageWithData:data]; [spinner hide:YES]; [spinner removeFromSuperViewOnHide]; }