ImageIO:< ERROR> JPEG损坏的JPEG数据:数据段iphone的过早结束 - 如何捕获这个? [英] ImageIO: <ERROR> JPEG Corrupt JPEG data: premature end of data segment iphone - how to catch this?

查看:250
本文介绍了ImageIO:< ERROR> JPEG损坏的JPEG数据:数据段iphone的过早结束 - 如何捕获这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过HTTP下载图像来获取此错误。我查看了在这里回答,但即使是有效的图片不要从函数返回 YES

I get this error by downloading an image by HTTP. I have looked at the answer here but even the valid images don't return YES from the function.

还有其他想法吗?

获取图像的代码非常简单。这发生在后台线程中。

The code to get the image is simple enough. This happens in a background thread.

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
UIImage *image = [UIImage imageWithData:data];

这是该线程的函数:

- (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;
}


推荐答案

使用unsigned char。然后比较应该工作。

Use an unsigned char. Then comparing should work.

const unsigned char * bytes = (const unsigned char *)[jpeg bytes];

而不是

const char * bytes = (const char *)[jpeg bytes];

这篇关于ImageIO:&lt; ERROR&gt; JPEG损坏的JPEG数据:数据段iphone的过早结束 - 如何捕获这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆