我正在从响应中下载视频文件。 我想显示HUD进度的下载进度条。 但我怎么能这样做。 我发送验证json到服务器和服务器验证发送回视频文件字节。我想通过使用HUD-progreasse bar显示下载百分比。
如果我调用[request setDidReceiveDataSelector:@selector(request:didReceiveBytes:)];
而不是显示我得到了多少字节但它没有将字节存储到缓存文件中(它不会将文件存储到手机中)
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[[NSURL alloc] initWithString:@"http://testing.io/dev.php/video/verifyReceipt"]];
[request setPostValue:resultAsString forKey:@"verify"];// sending json in post
[request setDidReceiveDataSelector:@selector(request:didReceiveBytes:)];
[request setDidFinishSelector:@selector(requestDone:)];
[request setTimeOutSeconds:120];
[request setDelegate:self];
[request setNumberOfTimesToRetryOnTimeout:2];
[request setDownloadProgressDelegate:self];
request.showAccurateProgress = YES;
[request startSynchronous];
}
-(void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data
{
[videoData appendData:data];// appending data with global NSmutabledata
NSLog(@"data is %@",data);
}
- (void)requestDone:(ASIHTTPRequest *)request{
//[MBProgressHUD hideHUDForView:self.view animated:YES];
// SAVED Video PATH
// Get the Document directory
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// Add your filename to the directory to create your saved video location
NSString* movLocation = [documentDirectory stringByAppendingPathComponent:[fileName stringByAppendingString:@".mov"]];
if(request.responseStatusCode==200)
{
[videoData writeToFile:movLocation atomically:NO];
NSLog(@"in request done sucsessfully downlaod and store in database %d",request.responseStatusCode);
[DBHelper savePurchaseId:fileName];
[self movieReceived];
}
else{
NSLog(@"in request downlaod and store in database failed %@",request.responseHeaders);
}
}
-(void)requestFailed:(ASIHTTPRequest *)request
{
NSLog(@"%@",request.error);
}
答案 0 :(得分:0)
fileUrl =下载视频的网址字符串。
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; operation.outputStream = [NSOutputStream outputStreamToFileAtPath:destPath append:NO]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { // Give alert that downloading successful. NSLog(@"Successfully downloaded file to %@", destPath); /* To call delegate to response the result. */ [self.target parserDidDownloadItem:destPath]; HUD.detailsLabelText = [NSString stringWithFormat:@"%@ %i%%",[JGlobals getLocalvalue:@"Downloading"],100]; [HUD hide:TRUE]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // Give alert that downloading failed NSLog(@"Error: %@", error); /* To call delegate to response the result. */ [self.target parserDidFailToDownloadItem:error]; [HUD hide:TRUE]; }]; [operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { // Progress float totalProgress = (totalBytesWritten / (totalBytesExpectedToWrite * 1.0f) * 100); HUD.detailsLabelText = [NSString stringWithFormat:@"Downloading %i%%", MIN((int)(totalProgress), 99)]; }]; [operation start];
答案 1 :(得分:0)
我正在考虑一种可能不正确的方法,但它可以帮助您避免编写大量代码。
首先将普通UIProgressView
实例设置为下载进度委托,如下所示
[request setDownloadProgressDelegate:uiprogreeViewInstance];
现在ASIHTTPRequest框架将在下载进行时更新该进度视图。 ProgressView有一个名为progress
的属性,范围从0.0到1.0。
现在实例化MBProgressHUD,将其添加为您想要的子视图。将progress
实例的MBProgressHUD
值设置为UIProgressView
的值{{1}}。隐藏uiprogressview实例。