Я пытаюсь загрузить небольшие PDF-файлы 1-2Mb, возможно, не более 5 МБ. Я работаю над классом SelectionViewController.m, который имеет UIProgressView, называемый progressBar, а также реализует протокол NSURLConnectionDataDelegate.Обновление индикатора выполнения для загрузки - NSURLConnectionDataDelegate didReceiveData получает только один раз
Самое смешное в том, что мой балл прогресса начинается с 0.0 до 1.0 внезапно. Я хотел бы постепенно увеличивать его, чтобы он выглядел красиво.
Вот мой код:
-(void)downloadPDFToMyDocumentsFrom:(NSString*) PDFUrl filename:(NSString *) title {
NSURL *url = [[NSURL alloc] initWithString:PDFUrl];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
NSLog(@"%@", self);
fileName = [title stringByAppendingPathExtension:@"pdf"];
filePath = [[SearchFeed pathToDocuments] stringByAppendingPathComponent:fileName];
}
//handling incoming data
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)recievedData{
if(self.data == nil)
{
self.data = [[NSMutableData alloc] initWithCapacity:2048];
}
[self.data appendData:recievedData];
NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.data length]];
float progress = [resourceLength floatValue] /[fileLength floatValue];
self.progressBar.progress = progress;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
long length = [response expectedContentLength];
fileLength = [NSNumber numberWithUnsignedInteger:length];
//lastProgress = 0.0;
//currentLength = 0.0;
NSLog(@"%f ------------------------------------------------------- is the fileLength", [fileLength floatValue]);
}
//handling connection progress
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
//WRITE code to set the progress bar to 1.0
//self.progressBar.progress = 1.0;
// [fileStream close];
[self.data writeToFile:filePath atomically:YES];
//[listFileAtPath:self.filePath];
connection = nil;
NSLog(@"%f %f-------------------------------------------- Finished Loading ", lastProgress, [fileLength floatValue]);
}
Я поставил некоторые заявления NSLog, чтобы помочь мне отлаживать и, похоже, его загрузки сразу.
Вот выход из всех NSLog:
2013-09-05 20:30:04.856 Revista[63246:c07] <SelectionViewController: 0x7180000>
2013-09-05 20:30:04.859 Revista[63246:c07] 63561.000000 ------------------------------------------------------- is the fileLength
2013-09-05 20:30:04.861 Revista[63246:c07] 0.000000 63561.000000-------------------------------------------- Finished Loading
ли вы, ребята, есть какие-либо идеи о том, как сделать его загрузки на куски и сделать прогресс бар двигаться плавно?