2011-08-10 101 views
0

在我的應用程序中,我必須將小於10 MB大小的圖像或視頻上傳到服務器。所以我需要在將圖像或視頻上傳到服務器之前檢查從圖像選擇器控制器中選擇的圖像或視頻的大小。自2天以來我一直在尋找答案。有沒有人可以幫助我這方面,提前感謝。我寫過像從圖片選擇器中選擇的字節中的圖像/視頻大小

 NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES); 
      NSString* fullPathToFile = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileName]; 
      NSLog(@"fullPathToFile:%@", fullPathToFile); 
      NSError *error = nil; 
      NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:fullPathToFile error:&error]; 

      if (!error) 
      { 
       NSNumber *size = [attributes objectForKey:NSFileSize]; 
       NSLog(@"File size: %@", size); 
      } 

但它返回null。

回答

0

您是否嘗試過使用- (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error並從返回的字典中使用密鑰filesize訪問大小。

+0

我如何獲得路徑 – Possible

1

最後我得到了我的問題解決。對於第一個,我們必須將該圖像/視頻轉換爲NSData,然後我們擁有像「length」這樣的屬性。 對於如:

fileData = UIImageJPEGRepresentation(image, 90); 
    //fileData = [NSData dataWithContentsOfURL:videoURL]; 
    NSUInteger fileLength = [fileData length]; 
    NSLog(@"fileLength:%u",fileLength); 

它以字節爲單位返回的大小。謝謝大家。