2013-10-08 121 views
4

我有我在其中保存文檔文件夾中的視頻文件它工作正常,但我想獲取保存文件的文件大小,我已經搜索,但沒有得到結果使用NSfileManager,這裏是我用來保存視頻的代碼。我想獲取文件大小並在UILabel上顯示它。如何從iPhone文檔文件夾中獲取文件的文件大小

感謝

 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*[email protected]"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; 

回答

11

試試這個它的幫助,你

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

這個文件大小將是KB或Bytes –

+1

這個文件大小以字節爲單位 –

+1

fileAttributesAtPath:traverseLink:已從iOS 6棄用 –

0

嘗試這種方式。

// 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); 
} 
+0

這個文件的大小是這有助於你? – user1673099

11

注:上述方法已過時,請使用以下方法

目的-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{} 

** 以字節爲單位

相關問題