【问题标题】:how to convert totalBytesExpectedToWrite to mega bytes in ios如何在ios中将totalBytesExpectedToWrite转换为兆字节
【发布时间】:2016-04-26 08:25:34
【问题描述】:

嗨,我是 ios 开发人员的新手。我正在使用 afnetworking 类下载 pdf 文件。我将成功下载 pdf 文件,但我想要这么多兆字节的文件大小,但问题是我的大小以字节为单位,但我想要兆字节字节。

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

【问题讨论】:

    标签: ios uitableview afnetworking


    【解决方案1】:

    在 iOS 6 及更高版本中,有一个非常方便的类用于此目的

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

    根据大小自动添加单位(KB、MB、GB)。

    【讨论】:

      【解决方案2】:

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

      NSString *bytes=[NSString stringWithFormat:@"%lld",totalBytesExpectedToWrite];
                      double byte=[bytes doubleValue];
      
                      NSLog(@"File size is : %.2f MB",(float)byte/1024.0f/1024.0f);
      

      【讨论】:

        猜你喜欢
        • 2011-01-22
        • 1970-01-01
        • 2018-06-24
        • 2016-06-08
        • 2023-04-02
        • 2010-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多