[英]Base64 decoding of a image gives a red question mark
我正在unity3d中使用base64字符串对Facebook缩略图图像数据进行编码和解码。 当用户没有个人资料图片时,所得图像为红色问号。 有什么方法可以识别出正在发送的无效图片,以便可以用我选择的默认图片替换它吗?
我正在使用C#中的Convert.FromBase64String(字符串编码)将字符串转换为图像数据。
我假设您使用某些API从URL检索base64编码的字符串?
在这种情况下,您只需将检索到的字符串转储到控制台一次,然后将其复制到源代码中,然后将其与将来获得的字符串进行比较即可。 当然,如果您使用的facebook API决定提供其他图标,这将中断,在这种情况下,您将必须转储新的“未知用户”缩略图。
string encoded = ... // however you obtain your thumbnail
print encoded; // dump the string to the console once. remove this statement later
if (encoded == "...here comes the (rather large) string you just copied")
encoded = "...here comes some other image you like to use, encoded as string";
...
不是很优雅,但至少易于实现。
我想,如果在不存在个人资料图片的情况下可预测地返回错误图像(例如“红色问号”图像),则可以简单地测试这种情况并替换为您选择的另一张图像。 您可以选择将红色问号图像存储为资源,然后将其Base64字符串与从您的请求返回的每个图像的Base64进行比较。
在这种情况下,关键的代码可能看起来像这样(假设您已经将图像的Base64字符串存储在资源中):
ResourceManager rm =
new ResourceManager("ExampleAppData", typeof(ExampleApp).Assembly);
String errorImageBase64 = rm.GetString("ErrorImageBase64");
// the image you get from your request
String resultImageBase64 = GetProfileImageBase64();
Image missingProfile;
if(resultImageBase64.Equals(errorImageBase64))
{
missingProfile = ImageFromBase64String(rm.GetString("MissingProfileBase64"));
}
else
{
missingProfile = ImageFromBase64String(resultImageBase64);
}
参考文献:
http://msdn.microsoft.com/zh-CN/library/2xsy4hac.aspx
http://ozgur.ozcitak.com/snippets/2009/12/21/base64-encoding-an-image-with-csharp.html
以我的经验,这是由于错误的请求。 在我的情况下,返回红色问号的错误API查询是/{fbId}/picture?g&type=square&height=128&width=128
我解决了删除“?g”的问题,现在的工作查询是/{fbId}/picture?type=square&height=128&width=128
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.