菜鸟教程小白 发表于 2022-12-12 13:09:07

iOS - 获取从照片卷轴中选择的图像的文件大小


                                            <p><p>我正在构建一个应用程序,允许用户从他们的设备中选择照片和视频并将它们上传到服务器。试图获取每个项目选择的文件大小(以字节为单位),有人可以帮我吗?</p>

<pre><code>if ( == ALAssetTypePhoto){ // image file
      if (){
            NSURL* urlPath=;

            item = ;
            item.itemImage = ;
            item.itemType = 1; // image
            item.itemSize = // what do I need here??
            ;
      }
    } else if ( == ALAssetTypeVideo){ // video file
      if (){
            NSURL* urlPath=;
            item = ;
            item.itemImage = ;
            item.itemType = 2; // video
            item.itemSize = // what do I need here??
            ;
      }
    }
</code></pre>

<p><strong>编辑</strong></p>

<p>通过视频获取 NSCocaoErrorDomain 256:</p>

<pre><code>            NSURL* urlPath=;

            item = ;
            item.itemImage = ;
            item.itemType = 2; // video

            //Error Container
            NSError *attributesError;
            NSDictionary *fileAttributes = [ attributesOfItemAtPath: error:&amp;attributesError];
            NSNumber *fileSizeNumber = ;
            long fileSize = ;
            item.itemSize = fileSize;

            ;
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>仅用于<strong>图像</strong>数据选择:</p>

<pre><code>item.itemImage = (UIImage*);
NSData *imgData = UIImageJPEGRepresentation(item.itemImage, 1); //1 it represents the quality of the image.
NSLog(@&#34;Size of Image(bytes):%d&#34;,);
</code></pre>

<p>希望这会对你有所帮助。</p>

<p>以下方法是泛化的,它适用于图像和视频:</p>

<p>这样的事情应该注意查找从 <strong>UIImagePickerController</strong></p> 返回的所选图像或视频的文件大小

<pre><code>- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

      NSURL *videoUrl=(NSURL*);

      //Error Container
      NSError *attributesError;
      NSDictionary *fileAttributes = [ attributesOfItemAtPath: error:&amp;attributesError];
      NSNumber *fileSizeNumber = ;
      long long fileSize = ;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iOS - 获取从照片卷轴中选择的图像的文件大小,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/27244714/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/27244714/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS - 获取从照片卷轴中选择的图像的文件大小