【发布时间】:2018-12-03 18:30:29
【问题描述】:
我想从服务器下载图像并显示它。我得到类型为 image/png 的标题,现在如何转换为图像?
我得到标题:
Cache-Control: public, max-age=86400 Connection: Keep-Alive Date: Fri, 2018 年 11 月 30 日 12:32:08 GMT Keep-Alive:超时 = 5,最大值 = 99 OkHttp-Received-Millis: 1543581128745 OkHttp-Selected-Protocol: http/1.1 OkHttp-Sent-Millis:1543581128514 服务器:Apache/2.4.33 (亚马逊) OpenSSL/1.0.2k-fips PHP/7.0.30 X-Powered-By: PHP/7.0.30 内容长度:26190 内容类型:image/png 保活:超时=5, max=99 OkHttp-Received-Millis: 1543581128745 OkHttp-Selected-Protocol: http/1.1 OkHttp-Sent-Millis: 1543581128514 X-Powered-By: PHP/7.0.30 }}
public async Task<AvatarModel> GetAvatar(string dpi, Int64 uin)
{
var response = await BasicRequestAsync(string.Format(AVATAR_URL, dpi, uin), HttpMethod.Get, true, null, null, false);
if(response == null) return null;
var stream = await response.Content.ReadAsStreamAsync();
.....
}
服务器: 标题:
IMToken:123123123
用户名:123
用户代理:...
返回带有标题的未打包图像 内容类型:图片/png
public class AvatarModel
{
public AvatarModel() { }
public AvatarModel(AvatarResponseModel model)
{
Id = model.Id;
Base64 = model.Base64;
}
public Int64 Id { get; set; }
private string base64;
public string Base64
{
get { return base64; }
set
{
base64 = value;
AvatarImage = Xamarin.Forms.ImageSource.FromStream(
() => new MemoryStream(Convert.FromBase64String(base64)));
}
}
public ImageSource AvatarImage { get; set; }
}
public class AvatarResponseModel
{
[JsonProperty("Id")]
public Int64 Id { get; set; }
[JsonProperty("base64")]
public string Base64 { get; set; }
}
【问题讨论】:
-
什么是 AvatarModel?那里的图像是如何表示的?
-
@Jason 我在上面添加了模型
-
从url下载的图片是否已经Base64编码,还是只是一个png?如果不需要的话,您似乎通过将其存储在 Base64 中来为自己创造额外的工作。通常,最有效的方法是将图像写入文件并将文件路径存储在模型中。
-
@Jason only png 但 base64 我必须使用另一个 webapi。 Whit base64 我没有问题