android - 在不下载图像的情况下计算图像的尺寸/大小

标签 android

有没有办法在不从服务器实际下载图像的情况下获取图像尺寸或大小。

就像图像托管在 https://500px.com/ 上一样 或 https://imgur.com/ 我想做一些计算,但我也想节省带宽。如果图像尺寸很大并且用户带宽不太好,我想将图像排队等待稍后使用。

假定 API 不提供此类信息。

最佳答案

在 NSURLConnection 的 didRecieveResponse 委托(delegate)方法中,您将收到包含此类信息的响应。当且仅当这些已在服务器上设置时:

以下获取图像的磁盘大小:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    int state = [httpResponse statusCode];

    if (state >= 400 && state <600)
    {
         // something wrong happen
    }

    NSLog(@"Download Response : %@", [response description]);  // this shows all the info. available by server. 

    int file_size = [response expectedContentLength];
}

您可以检查大小,然后根据需要取消连接。

For image dimensions. check this question. the answer claims that it can be done with the suggested fastimage category.

完成此操作的唯一合乎逻辑的方法是下载包含图像标题的图像的第一部分。其中包含图像类型(png、jpg ...)和图像尺寸等信息。我相信这个图书馆会这样做。

关于android - 在不下载图像的情况下计算图像的尺寸/大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32039211/

相关文章:

android - 如何在 android 中的单个 ImageView 中添加两个渐变(顶部-底部和底部-顶部)?

安卓工作室 : Regex not working

android - 10.0.2.2 :8080 in web browser in emulator is not working

android - 在 Android 上没有 dispatch_main 的 libDispatch 服务于主队列

java - 可区分的套接字输入

java - Android如何取消绘图前清除?

android - Samsung Galaxy S 在 Launcher 中具有图标背景,有什么方法可以指定它?

javascript - 深色模式干扰背景颜色

java - OkHttp3 - 使用 HttpURL.Builder 的 boolean 查询参数

Android USB 信用卡读卡器