2013-05-29 46 views
0

我正在使用ASIHttpRequest和MB-HUD进度库,现在我想要显示MBHUD进度条来下载视频文件。 我很困惑这两个library.they的文档是不够的初学者。MBHUD进度条加ASIHTTPRequest进行跟踪下载

这是我使用的应用程序购买库中的代码。我不知道如何ü可以实现以下ASSiHttpRequest的方法来道我的下载既库

- (void) completeTransaction: (SKPaymentTransaction *)transaction 
{ 
NSLog(@"Transaction Completed"); 
// You can create a method to record the transaction. 
// [self recordTransaction: transaction]; 

// You should make the update to your app based on what was purchased and inform user. 
// [self provideContent: transaction.payment.productIdentifier]; 

// Finally, remove the transaction from the payment queue. 

HUD = [[MBProgressHUD alloc] initWithView:self.view]; 
[self.view addSubview:HUD]; 

// Set determinate mode 
HUD.mode = MBProgressHUDModeAnnularDeterminate; 
//HUD.delegate = self; 
HUD.labelText = @"Downloading"; 

// myProgressTask uses the HUD instance to update progress 
[HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES]; 

[self downloadFromURL:[NSURL URLWithString:@"http://techslides.com/demos/sample-videos/small.mp4"]]; 
[[SKPaymentQueue defaultQueue] finishTransaction: transaction]; 
    } 

    -(void)downloadFromURL:(NSURL *)url 
    { 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
    filePath = [documentsDirectory stringByAppendingPathComponent:@"small.mp4"]; 
    NSLog(@"Path is %@",filePath); 
    [request setDelegate:self]; 
    [request setTimeOutSeconds:60]; 
    [request setNumberOfTimesToRetryOnTimeout:2]; 
    [request setDownloadDestinationPath:filePath]; 
    [request startAsynchronous]; 
    [request setDownloadProgressDelegate:self]; 
    request.showAccurateProgress = YES; 
    } 


    - (void)request:(ASIHTTPRequest *)request incrementDownloadSizeBy:(long long)newLength 
    { 
    NSLog(@"newLength: %lld", newLength); 
    } 

    -(void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes { 
    NSLog(@"Received bytes:%lld",bytes); 

     } 

    -(void)requestFinished:(ASIHTTPRequest *)request 

    { 
[MBProgressHUD hideHUDForView:self.view animated:YES]; 
[self movieReceived]; 

    } 

    -(void)requestFailed:(ASIHTTPRequest *)request 
    { 

NSLog(@"%@",request.error); 

    } 


    -(void)movieReceived 
     { 
    playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL]; 
    } 


- (void)myProgressTask { 
// This just increases the progress indicator in a loop 
float progress = 0.0f; 
while (progress < 1.0f) { 
    progress += 0.01f; 
    HUD.progress = progress; 
    usleep(60000); 
} 
    } 

我不知道我怎样才能改变myprogresstask的所有值根据文件的大小来更新进度条..

任何帮助表示赞赏。

+0

你为什么不使用进度栏下载嵌入ASIHTTPReaquest目的。 –

+0

我不知道ASIHTTPRequest里面有内置的进度条......但是MBHUDProgress酒吧很时尚,看起来不错,所以我用MBHUD进度条 –

+0

Ofcourse进度条在那里。在我的答案中查找, –

回答

2

计算对总字节数接收的字节数的比例

-(void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes

和设置进度值

HUD.progress = normalisedPercentage;

+0

如果文件大小未知,该怎么办。 。我怎么计算它 –

+0

然后你不能显示进度。如果你不知道你下载的文件大小,你将如何显示进度。 – Vignesh

+0

我以为会有一些方法可以使用我们可以显示进度条...如果我们知道文件大小比我应该做什么?? .. didReceiveBytes:(long long)在这个方法中的字节字节?..我应该将它们添加到另一个字节变量,并检查它是否等于我的文件大小,并根据该添加百分比HUD.progress ?? ..你可以请编辑你的答案简要请 –

1

显示进度条进行下载。

ASINetworkQueue *requestUserSpecificFileQueue = [[ASINetworkQueue alloc] init]; 
[requestUserSpecificFileQueue reset]; 
UIProgressView *progressView=[[UIProgressView alloc]initWithFrame:CGRectMake(5, 190, 200, 20)]; 
[ViewSelected addSubview:progressView]; 
[requestUserSpecificFileQueue setDownloadProgressDelegate:progressView]; 
[requestUserSpecificFileQueue setRequestDidFinishSelector:@selector(downLoadFinished:)]; 
[requestUserSpecificFileQueue setRequestDidFailSelector:@selector(downloadFailed:)]; 
[requestUserSpecificFileQueue setShowAccurateProgress:YES]; 
[requestUserSpecificFileQueue setDelegate:self]; 
ASIHTTPRequest *requestUserSpecificFile=[ASIHTTPRequest requestWithURL:[dict objectForKey:@"object"]]; 
requestUserSpecificFile.tag=bookViewSelected.tag; 
[requestUserSpecificFile setDownloadDestinationPath:[[self returnUnzipBundlePathName] stringByAppendingPathComponent:dictLocal[@"fullLengthFilename"]]]; 

如果__IPHONE_OS_VERSION_MAX_ALLOWED> = __IPHONE_4_0

[requestUserSpecificFile setShouldContinueWhenAppEntersBackground:YES]; 

ENDIF

[requestUserSpecificFileQueue addOperation:requestUserSpecificFile]; 
[requestUserSpecificFileQueue go]; 
+0

你能告诉我我应该在哪里我的URL在asiNetworkQueue?..因为我已经使用ASIHTTPRequest的URL下载..和你正在使用网络队列.. –

+0

ASIHTTPRequest * requestUserSpecificFile = [ASIHTTPRequest requestWithURL:[dict objectForKey:@“object”]]; requestUserSpecificFile.tag = bookViewSelected.tag; [requestUserSpecificFile setDownloadDestinationPath:[[self returnUnzipBundlePathName] stringByAppendingPathComponent:dictLocal [@“fullLengthFilename”]]]; #if __IPHONE_OS_VERSION_MAX_ALLOWED> = __IPHONE_4_0 [requestUserSpecificFile setShouldContinueWhenAppEntersBackground:YES]; #endif [requestUserSpecificFileQueue addOperation:requestUserSpecificFile]; [requestUserSpecificFileQueue go];使用此代码的URL。 –

+0

可以请你编辑答案..我不明白它 –