有没有办法在不从服务器实际下载图像的情况下获取图像尺寸或大小。
就像图像托管在 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/