2016-04-26 5 views
0

Hii Ich bin neu in IOS developer.Ich arbeite, um PDF-Datei mit afnetworking Klasse.I herunterladen wird erfolgreich herunterladen PDF-Datei, aber ich möchte so Megabyte für die Dateigröße, aber das Problem ist ich habe Größe in Byte, aber ich möchte in Megabyte.Wie konvertiert man totalBytesExpectedToWrite in Mega Bytes in 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** 
    } 

Fehler Bad Empfängertyp 'int64_t' (auch bekannt als 'long long') ist. Bitte mein Problem

Antwort

1

In iOS 6 und später gibt es eine sehr günstige Klasse für genau diesen Zweck

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

Die Einheit (KB, MB, GB) automatisch hinzugefügt wird, abhängig von der Größe.

0

ich bin lösen meine problem.convert totalBytesExpectedToWrite zu NSString lösen.

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

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