如何从iPhone文档文件夹中获取文件的文件大小 [英] How to get file size of file from iPhone documents folder

查看:179
本文介绍了如何从iPhone文档文件夹中获取文件的文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have in which i save video file in documents folder it works fine,but i want to get the file size of the saved file,I have searched but did not get result using NSfileManager,here is the code which i use for saving video.I want to get the file size and show it on 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;


推荐答案

试试这个帮助你

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

这篇关于如何从iPhone文档文件夹中获取文件的文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆