且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查 NSData 是否适用于 UIImage

更新时间:2023-11-27 11:48:34

如上所述 在文档中 imageWithData:如果 UIImage 无法从数据创建图像,则返回 nil.在运行时处理这个问题的正确方法是在该方法返回 nil 时提供一个占位符图像.

As stated in the documentation imageWithData: returns nil if UIImage could not create an image from the data. The correct way to handle this at runtime is to provide a placeholder image when that method returns nil.

至于诊断,首先查看控制台消息,它们有时会提供有用的信息.如果没有,您***的运气是将 NSData 转储到磁盘并将结果与​​存储在服务器上的文件进行比较.有时您会收到损坏的数据,有时您会在您期望 jpeg 二进制数据的地方收到一条 html 错误消息.

As for the diagnostic, first look at the console messages, they sometimes provide useful info. If not, your best luck is to dump the NSData to disk and compare the result to the file stored on the server. You sometimes get corrupted data, you sometimes just get an html error message where you were expecting jpeg binary data.