在网络不好时,可能请求的图片数据不完整,这时候在imageView展示图片时,因为发现PNG图片不完全正确,所以早console里报错,如下:
<Error>: ImageIO: PNG IDAT: invalid stored block lengths
但这个不是运行时异常,不知道该怎么捕获,从而进行例如重新请求等操作。求解该怎么做?
在网络不好时,可能请求的图片数据不完整,这时候在imageView展示图片时,因为发现PNG图片不完全正确,所以早console里报错,如下:
<Error>: ImageIO: PNG IDAT: invalid stored block lengths
但这个不是运行时异常,不知道该怎么捕获,从而进行例如重新请求等操作。求解该怎么做?
图片能正常显示么?
如果不能正常显示的话,你可以自己先做一个验证操作,如果自己验证过了再赋值给imageView,这种错误也不该无视吧?毕竟图片确实有问题。
如果你实在想让它不显示,我觉得有三条个办法:
so上找到的检测 PNG 的方法:
- (BOOL)dataIsValidPNG:(NSData *)data
{
if (!data || data.length < 12)
{
return NO;
}
NSInteger totalBytes = data.length;
const char *bytes = (const char *)[data bytes];
return (bytes[0] == (char)0x89 && // PNG
bytes[1] == (char)0x50 &&
bytes[2] == (char)0x4e &&
bytes[3] == (char)0x47 &&
bytes[4] == (char)0x0d &&
bytes[5] == (char)0x0a &&
bytes[6] == (char)0x1a &&
bytes[7] == (char)0x0a &&
bytes[totalBytes - 12] == (char)0x00 && // IEND
bytes[totalBytes - 11] == (char)0x00 &&
bytes[totalBytes - 10] == (char)0x00 &&
bytes[totalBytes - 9] == (char)0x00 &&
bytes[totalBytes - 8] == (char)0x49 &&
bytes[totalBytes - 7] == (char)0x45 &&
bytes[totalBytes - 6] == (char)0x4e &&
bytes[totalBytes - 5] == (char)0x44 &&
bytes[totalBytes - 4] == (char)0xae &&
bytes[totalBytes - 3] == (char)0x42 &&
bytes[totalBytes - 2] == (char)0x60 &&
bytes[totalBytes - 1] == (char)0x82);
}
Objective-C的hook方案(一): Method Swizzling
IOS应用发布NSLog的注释及使用重定向,把控制台内容写入文件
PNG validation on iOS
2 回答1k 阅读
1 回答1.1k 阅读✓ 已解决
1 回答2.7k 阅读
1 回答1.5k 阅读
1 回答1.4k 阅读
1.7k 阅读
1 回答1.1k 阅读
真是功夫不负有心人,在各种stackoverflow和google无解之后,我自己读了UIImage、UIImageView以及ImageIO的源码和官方说明,找到了个取巧的办法,你需要写下面两步代码:
1.读取图片数据,然后调用2中的方法进行校验
2.判断是否为有效的PNG图片,JPG的请使用UIImageJPEGRepresentation方法
真是眼泪都留下来了!~