【问题标题】:How to get file size of file from iPhone documents folder如何从 iPhone 文档文件夹中获取文件的文件大小
【发布时间】:2013-10-14 21:17:50
【问题描述】:

我将视频文件保存在文档文件夹中它工作正常,但我想获取保存文件的文件大小,我已经搜索但没有使用 NSfileManager 得到结果,这是我使用的代码保存视频。我想获取文件大小并在 UILabel 上显示。

谢谢

       NSURL*videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

       NSLog(@"found a video");

       videoData = [[NSData dataWithContentsOfURL:videoURL] retain];
       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       NSString *documentsDirectory = [paths objectAtIndex:0];

       NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
      [dateFormat setDateFormat:@"dd-MM-yyyy_HH:mm:SS"];
      NSDate *now = [[[NSDate alloc] init] autorelease];
      NSDate* theDate = [dateFormat stringFromDate:now];



      NSString*myDate=[dateFormat stringFromDate:theDate];
      NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Default Album"];

     if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];



    NSString*test=@"test";


    NSString*testUser=[test stringByReplacingOccurrencesOfString:@" " withString:@""];



    videopath= [[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/%@.mp4",documentsDirectory,testUser]] autorelease];

    BOOL success = [videoData writeToFile:videopath atomically:NO];
    NSLog(@"Successs:::: %@", success ? @"YES" : @"NO");
            NSLog(@"video path --> %@",videopath);

            NSURL *movieURL = [NSURL fileURLWithPath:videopath];
    AVURLAsset *avUrl = [AVURLAsset assetWithURL:movieURL];
    CMTime time1 = [avUrl duration];
    int seconds = ceil(time1.value/time1.timescale);

    //  durationTime=[NSString stringWithFormat:@"%d",seconds];

    //  insertTime=[NSString stringWithFormat:@"%d",seconds];



    NSString*messageA=[NSString stringWithFormat:@"You have recorded video of duration of %d seconds  ",seconds];


    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:messageA
                                                   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];



    AVAsset *asset = [AVAsset assetWithURL:movieURL];// url= give your url video here
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
    CMTime time = CMTimeMake(1, 5);//it will create the thumbnail after the 5 sec of video
    CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
    UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
    thumbnailImageView.image=thumbnail;

【问题讨论】:

    标签: iphone ios documents


    【解决方案1】:

    注意:以上方法已弃用,请使用以下方法

    目标-C:

        NSError* error;
        NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:mediaURL error: &error];
        NSNumber *size = [fileDictionary objectForKey:NSFileSize]; 
    

    斯威夫特:

    do
    {
       let fileDictionary = try FileManager.default.attributesOfItem(atPath: urlString)
       let fileSize = fileDictionary[FileAttributeKey.size]
       print ("\(fileSize)")
    } 
    catch{}
    

    ** 此文件大小(以字节为单位)

    【讨论】:

      【解决方案2】:

      试试这个对你有帮助

       NSDictionary *fileDictionary = [[NSFileManager defaultManager] fileAttributesAtPath:videopath traverseLink:YES];
           fileSize = [fileDictionary fileSize];
      

      【讨论】:

      • 此文件大小为 KB 或字节
      • 此文件大小(以字节为单位)
      • fileAttributesAtPath:traverseLink: 已从 iOS 6 中弃用
      • 您还使用了 use attributesOfItemAtPath:error: 方法,它也给出了相同的结果。
      • 方法已弃用。使用 - attributesOfItemAtPath:filePath error:nil
      【解决方案3】:

      使用NSFileManager attributesOfItemAtPath:error

      https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/attributesOfItemAtPath:error:

      它返回一个文件属性的 NSDictionary,它包含的一个键是 NSFileSize,文件的大小。

      【讨论】:

        【解决方案4】:

        试试这个方法。

            // Get file size
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *filePath = [documentsDirectory stringByAppendingString:[directoryContent objectAtIndex:indexPath.row]];
        NSDictionary *fileAttributes = [fileManager fileAttributesAtPath:filePath traverseLink:YES];
        if(fileAttributes != nil)
        {
        NSString *fileSize = [fileAttributes objectForKey:NSFileSize];
        [[cell detailTextLabel] setText:[NSString stringWithFormat:@"%@ kb", fileSize]];
        NSLog(@"File size: %@ kb", fileSize);
        }
        

        【讨论】:

        • 这对您有帮助吗?
        【解决方案5】:

        前几天我也在寻找非常相似的东西。我需要将本地文件上传到保管箱,这对我有用:检查以下链接:

        https://stackoverflow.com/a/1694928/2098401

        【讨论】:

          猜你喜欢
          • 2021-07-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-01
          • 1970-01-01
          • 2011-02-12
          • 1970-01-01
          • 2011-10-09
          相关资源
          最近更新 更多