如何在 [UIImage initWithData:] 中获取错误/警告

发布于 2024-11-25 08:56:04 字数 361 浏览 1 评论 0原文

我有一个基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风和你 2024-12-02 08:56:04

在堆栈溢出上有一个与此类似的线程:捕获错误:损坏的 JPEG 数据:数据段过早结束

解决方案是检查头字节 FF D8 和结束字节 FF D9。因此,如果 NSData 中有图像数据,您可以像这样检查它:

- (BOOL)isJPEGValid:(NSData *)jpeg {
    if ([jpeg length] < 4) return NO;
    const char * bytes = (const char *)[jpeg bytes];
    if (bytes[0] != 0xFF || bytes[1] != 0xD8) return NO;
    if (bytes[[jpeg length] - 2] != 0xFF || bytes[[jpeg length] - 1] != 0xD9) return NO;
    return YES;
}

然后,要检查 JPEG 数据是否无效,只需编写:

if (![self isJPEGValid:myData]) {
    NSLog(@"Do something here");
}

希望这有帮助!

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 bytes FF D9. So, if you have image data in an NSData, you can check it like so:

- (BOOL)isJPEGValid:(NSData *)jpeg {
    if ([jpeg length] < 4) return NO;
    const char * bytes = (const char *)[jpeg bytes];
    if (bytes[0] != 0xFF || bytes[1] != 0xD8) return NO;
    if (bytes[[jpeg length] - 2] != 0xFF || bytes[[jpeg length] - 1] != 0xD9) return NO;
    return YES;
}

Then, to check if JPEG data is invalid, just write:

if (![self isJPEGValid:myData]) {
    NSLog(@"Do something here");
}

Hope this helps!

愚人国度 2024-12-02 08:56:04

在这种情况下,initWithData: 方法应返回 nil

尝试 :

UIImage *myImage = [[UIImage alloc] initWithData:imgData];
if(!myImage) {
    // problem
}

The initWithData: method should return nil in such cases.

Try :

UIImage *myImage = [[UIImage alloc] initWithData:imgData];
if(!myImage) {
    // problem
}
你与昨日 2024-12-02 08:56:04

我在这种情况下遇到了同样的问题。

事实证明,我正在将 NSMutableData 的实例传递到全局队列进行解码。在解码期间,NSMutableData 中的数据被从网络接收到的下一帧覆盖。

我通过传递数据副本修复了错误。使用缓冲池来提高性能可能会更好:

    NSData *dataCopy = [_receivedData copy];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    ZAssert([self isJPEGValid:dataCopy], @"JPEG data is invalid"); // should never happen
    UIImage *image = [UIImage imageWithData:dataCopy];
    dispatch_async(dispatch_get_main_queue(), ^{
        // show image
    });
});

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:

    NSData *dataCopy = [_receivedData copy];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    ZAssert([self isJPEGValid:dataCopy], @"JPEG data is invalid"); // should never happen
    UIImage *image = [UIImage imageWithData:dataCopy];
    dispatch_async(dispatch_get_main_queue(), ^{
        // show image
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文