如何在ios中将totalBytesExpectedToWrite转换为兆字节

时间:2016-04-26 08:25:34

标签: ios uitableview afnetworking

我是ios developer.i的新手。我正在使用afnetworking class.i工作下载pdf文件将成功下载pdf文件,但我想要文件大小的兆字节,但问题是我在字节中有大小但我想在mega字节。

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' (又名' long long')。请解决我的问题

2 个答案:

答案 0 :(得分:1)

在iOS 6及更高版本中,为了这个目的,有一个非常方便的类

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

根据尺寸自动添加单位(KB,MB,GB)。

答案 1 :(得分:0)

我解决了我的问题。将totalBytesExpectedToWrite转换为nsstring。

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

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