2016-04-26 121 views
0

HII我developer.i我的工作使用afnetworking class.i將全成下載PDF文件下載PDF文件IOS是新的,但我要爲文件大小,但問題,所以兆字節是我得到字節大小,但我想在兆字節。如何totalBytesExpectedToWrite轉換爲兆字節在IOS

CGPoint cursorPosition = [sender convertPoint:CGPointZero toView:self.tbl_subject]; 
    NSIndexPath *indexPath = [self.tbl_subject indexPathForRowAtPoint:cursorPosition]; 

    yearcell *currentCell = (yearcell *)[self.tbl_subject cellForRowAtIndexPath:indexPath]; 
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 

    NSURL *URL = [NSURL URLWithString:@"http://five-point-someone-chetan-bhagat_ebook.pdf"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 

    NSString *filename=[NSString stringWithFormat:@"%@.pdf",currentCell.lbl_title.text]; 
    NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]; 
    NSString* foofile = [stringPath stringByAppendingPathComponent:filename]; 
    if(![[NSFileManager defaultManager] fileExistsAtPath:foofile]) 
    { 

     NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { 

      NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; 

      return [documentsDirectoryURL URLByAppendingPathComponent:filename]; 

     } 
                   completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { 
                    NSLog(@"File downloaded to: %@", filePath); 
                   }]; 

     [manager setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) { 

      dispatch_async(dispatch_get_main_queue(), ^{ 
       // here is what you want 
       NSLog(@"totalbytes=%lld",totalBytesWritten); 


       NSString *readable = [NSString stringWithFormat:@"%lld MB", ([totalBytesExpectedToWrite longLongValue]/1024/1024)]; 
       float prog = (totalBytesWritten/(totalBytesExpectedToWrite * 1.0f) * 100); 
       [currentCell.p_progress setProgress:prog]; 

          }); 

     }]; 
     [downloadTask resume]; 
     [currentCell.downloadbutton reset]; 

    } 
    else 
    { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"GTU Paper" 
                  message:@"File alreay Exist" 
                  delegate:nil 
                cancelButtonTitle:@"Ok" 
                otherButtonTitles:nil]; 
     [alertView show]; 
     [currentCell.downloadbutton reset]; 
     // ur code here** 
    } 

錯誤是壞的接收器類型 '的int64_t'(又名 '長長')。請解決我的問題

回答

1

在iOS 6中和以後存在用於正是目的

NSString *bytes = [NSByteCountFormatter stringFromByteCount:totalBytesExpectedToWrite countStyle:NSByteCountFormatterCountStyleFile]; 
NSLog(@"File size is : %@", bytes); 

單元(KB,MB,GB)是依大小自動添加一個非常方便的類。

0

我是解決我problem.convert totalBytesExpectedToWrite到的NSString。

NSString *bytes=[NSString stringWithFormat:@"%lld",totalBytesExpectedToWrite]; 
       double byte=[bytes doubleValue]; 

       NSLog(@"File size is : %.2f MB",(float)byte/1024.0f/1024.0f);