2016-04-26 3 views
0

HII私はclass.iがダウンロードPDFファイルを成功しますafnetworking使用してPDFファイルをダウンロードするために働いていますdeveloper.i IOSに新しいですが、私は、ファイルサイズが、問題のためにそうメガバイトをしたい私ですバイトでサイズが取得されましたが、メガバイトが必要です。IOSのメガバイトにtotalBytesExpectedToWriteを変換する方法

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** 
    } 

エラーは、私はNSStringのに私のproblem.convertのtotalBytesExpectedToWriteを解決しています私の問題

答えて

1

以降まさにその目的

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

のための非常に便利なクラスが存在する単位(KB、MB、GB)のサイズに応じて自動的に追加されます。

0

を解決してください。不正な受信機タイプ 'int64_tの'(別名 '長い長い')です。 IOSの6では

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

       NSLog(@"File size is : %.2f MB",(float)byte/1024.0f/1024.0f); 
関連する問題