1

我正在尝试将图像从 Web API 返回到 zebble,如下所示:

网页接口:

    public HttpResponseMessage GetImage()
    {
        var memoryStream = custom logic to create image

        var result = new HttpResponseMessage(HttpStatusCode.OK);
        result.Content = new ByteArrayContent(memoryStream.ToArray());
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
        return result;
    }

斑块:

var imageData = await Get<byte[]>(url);
var imageView = new ImageView { ImageData = imageData }

但是Get抛出异常:

Exception thrown: 'Newtonsoft.Json.JsonReaderException' in Newtonsoft.Json.dll
Exception thrown: 'Newtonsoft.Json.JsonReaderException' in Newtonsoft.Json.dll
Exception thrown: 'System.Exception' in Zebble.UWP.dll
Exception thrown: 'System.Exception' in mscorlib.ni.dll
ERROR: HttpGet -> 'url' failed.
WARNING: Failed to convert API response to Byte[]

###############################################
Base issue: Unexpected character encountered while parsing value: �. Path '', line 1, position 1.

--------------------------------------
STACK TRACE:

at Zebble.Framework.BaseApi.<GetFromTheServer>d__24`1.MoveNext()

   at Zebble.Framework.BaseApi.<Get>d__25`1.MoveNext()

知道如何解决这个问题吗?

4

1 回答 1

1

您的 WebApi 必须返回string。您可以使用 Base64 字符串来传输图像数据。

第 1 步:将您的 Web API 代码更改为:

var memoryStream = new MemoryStream(); //TODO: custom logic to create image    
image.Save(memoryStream, ImageFormat.Png);
return Ok(Convert.ToBase64String(memoryStream.ToArray()));

更多细节:http: //zebble.net/docs/get-apis

第 2 步:在您的 Zebble 代码中,使用 Zebble WebAPI 代理调用您的 WebAPI 以接收返回的 Base64 字符串,转换为 byte[] 并设置为 ImageView 的源:

var base64 = await Api.Get<string>(url);
var imageData = Convert.FromBase64String(base64);
myImageView.ImageData = imageData;

更多细节:http: //zebble.net/docs/calling-a-get-api-in-the-mobile-app

于 2017-04-12T10:40:20.090 回答