【问题标题】:Image/Video Size in Bytes selected from image picker从图像选择器中选择的图像/视频大小(以字节为单位)
【发布时间】:2011-08-10 11:26:17
【问题描述】:

在我的应用程序中,我必须将小于 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。

【问题讨论】:

    标签: iphone upload uiimagepickercontroller filesize


    【解决方案1】:

    终于解决了我的问题。对于第一个,我们必须将该图像/视频转换为 NSData,然后我们拥有像“长度”这样的属性。 例如:

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

    它以字节为单位返回大小。谢谢大家。

    【讨论】:

      【解决方案2】:

      您是否尝试使用- (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error 并从返回的字典中使用键filesize 访问大小。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-19
        • 2023-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-21
        相关资源
        最近更新 更多