如何在 [UIImage initWithData:] 中获取错误/警告
我有一个基于 RTSP/UDP 的 MJPEG 流,我想用 [UIImage initWithData:] 为 UIImageView 生成 JPEG。大多数情况下,这效果很好,但有时我会收到损坏的图像和日志消息,例如:
ImageIO: <ERROR> JPEGCorrupt JPEG data: premature end of data segment
我的问题是:我如何才能看到(在运行时)发生此类消息?不幸的是“initWithData”没有错误输出,还有其他方法吗?
谢谢。
编辑:在这种情况下, initWithData 确实返回一个有效的 UIImage 对象,而不是 nil!
i have an MJPEG stream over RTSP/UDP from which i want to generate JPEGs for a UIImageView with [UIImage initWithData:]. Most of the time this works good, but sometimes i get corrupt images and log messages like:
ImageIO: <ERROR> JPEGCorrupt JPEG data: premature end of data segment
My Question is: how can i see (during runtime), that such message occurs? Unfortunatly 'initWithData' has no error output, is there any other way?
Thank you.
Edit: in this case, the initWithData does return a valid UIImage object, not nil!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在堆栈溢出上有一个与此类似的线程:捕获错误:损坏的 JPEG 数据:数据段过早结束。
解决方案是检查头字节
FF D8
和结束字节FF D9
。因此,如果 NSData 中有图像数据,您可以像这样检查它:然后,要检查 JPEG 数据是否无效,只需编写:
希望这有帮助!
There is a similar thread to this one on stack overflow: Catching error: Corrupt JPEG data: premature end of data segment.
There solution is to check for the header bytes
FF D8
and ending bytesFF D9
. So, if you have image data in an NSData, you can check it like so:Then, to check if JPEG data is invalid, just write:
Hope this helps!
在这种情况下,
initWithData:
方法应返回nil
。尝试 :
The
initWithData:
method should returnnil
in such cases.Try :
我在这种情况下遇到了同样的问题。
事实证明,我正在将 NSMutableData 的实例传递到全局队列进行解码。在解码期间,NSMutableData 中的数据被从网络接收到的下一帧覆盖。
我通过传递数据副本修复了错误。使用缓冲池来提高性能可能会更好:
I've encountered the same problem in this exact situation.
It turned out that I was passing an instance of NSMutableData to global queue for decoding. During decoding the data in NSMutableData was overwritten by next frame received from network.
I've fixed the errors by passing a copy of the data. It might be better to use a buffer pool to improve performance: