2013-06-04 152 views
1

我正在從亞馬遜s3下載圖像的iOS應用程序。我試圖跟蹤圖片下載的進度。亞馬遜s3 ios下載進度條

我不能讓-(void)request:(AmazonServiceRequest *)request didSendData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite委託方法觸發。

這是我至今設置委託方法的代碼。

-(void) viewDidLoad 
{ 
self.s3 = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY]; 
self.s3.endpoint = [AmazonEndpoints s3Endpoint:US_WEST_2]; 

NSString *key = [[NSString alloc] initWithFormat:@"path1/%@", uniqueID]; 

S3GetObjectRequest *downloadRequest = [[S3GetObjectRequest alloc] initWithKey:key withBucket: PICTURE_BUCKET]; 
[downloadRequest setDelegate:self]; 

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
hud.labelText = @"Loading Picture..."; 
S3GetObjectResponse *downloadResponse = [s3 getObject:downloadRequest]; 
} 

-(void)request:(AmazonServiceRequest *)request didSendData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite 
{ 
NSLog(@"Bytes Written: %i", bytesWritten); 
NSLog(@"Total Bytes Written: %i", totalBytesWritten); 
NSLog(@"Total Bytes Expected to Write: %i", totalBytesExpectedToWrite); 
} 

我設法讓這個委託方法工作上傳圖像,但似乎無法讓它下載工作。爲了追蹤下載進度,我需要做什麼不同的事情?

感謝

+0

希望這個鏈接將幫助你! http://docs.aws.amazon.com/mobile/sdkforios/developerguide/s3transfermanager.html#track-progress – arunjos007

回答

3

我碰到這個而來到AWS研究自己,我想我會發佈一個答案。 -(void)request:(AmazonServiceRequest *)request didSendData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite 僅在按名稱發送數據時有效。

如果您對文件的大小有一個大概的瞭解(您可以設置某種服務器請求以在開始下載之前獲取此信息,或者有一個典型數量)。然後,您可以使用-(void)request:(AmazonServiceRequest *)request didReceiveData:(NSData *)data,然後通過調用[self.data appendData:data]繼續將數據附加到的,然後測量self.data.length,它將字節數返回到您可以轉換爲字節的元數據大小估計值。

希望這會有所幫助!

1

AdamG是對的。

-(void)request:(AmazonServiceRequest *)request didSendData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten totalBytesExpectedToWrite:(long long)totalBytesExpectedToWrite僅用於上傳。

當你要跟蹤的下載進度,你應該使用:

-(void)request:(AmazonServiceRequest *)request didReceiveData:(NSData *)data

在這裏,我wan't添加一些我自己的colaboration。如果你想知道下載文件的大小,這是一個很好的方法。

S3GetObjectMetadataRequest *getMetadataObjectRequest = [[S3GetObjectMetadataRequest alloc] initWithKey:YOUR_KEY withBucket:YOUR_BUCKET]; S3GetObjectMetadataResponse *metadataResponse = [[AmazonClientManager s3] getObjectMetadata:getMetadataObjectRequest]; NSString *filesizeHeader = metadataResponse.headers[@"Content-Length"]; fileSize = [filesizeHeader floatValue];

我發現documentation是有點無語這一點。

此外,AWS iOS Samples也不包含一個很好的例子。實際上,有一條評論指出:「下載進度條只是一個估計值,爲了準確地反映進度條,你需要首先檢索文件大小」,但不知道如何去做。

所以,我發現這種方式通過搞亂getMetadataObjectRequest.debugDescription財產。

希望這會有所幫助!

+0

剛發現可以使用'metadataResponse.contentLength' –