经常在网络不太好时下载的png图片会出现模糊或者不完整现象,如何在ios开发里避免活着检测出图片已经损坏呢?如下图:
PNG的每个数据块都保有CRC位以供检验,算法是:
x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1
In PNG, the 32-bit CRC is initialized to all 1's, and then the data from each byte is processed from the least significant bit (1) to the most significant bit (128). After all the data bytes are processed, the CRC is inverted (its ones complement is taken). This value is transmitted (stored in the datastream) MSB first. For the purpose of separating into bytes and ordering, the least significant bit of the 32-bit CRC is defined to be the coefficient of the x31 term.
Practical calculation of the CRC often employs a precalculated table to accelerate the computation http://www.w3.org/TR/PNG-CRCAppendix.html
当然,还是得看图片源靠谱不靠谱,如果图片源偷懒,CRC位全部置1就没辙了=w=。
2 回答1k 阅读
1 回答1.1k 阅读✓ 已解决
1 回答2.7k 阅读
1 回答1.5k 阅读
1 回答1.4k 阅读
1.7k 阅读
1 回答1.1k 阅读
我读了下MKNetworkKit源码和要求,发现使用到了ImageIO.framework,这个框架应该对图像数据做了序列处理,不然就可能出现数据混乱。现在在网络极差的情况下也暂时没有发现这种情况。
2014年11月26日补充:
我发现引入ImageIO后,图片仍然有时候会损坏。在我就快要放弃了的时候,真是功夫不负有心人,在各种stackoverflow和google无解之后,我自己读了UIImage、UIImageView以及ImageIO的源码和官方说明,找到了个取巧的办法,你需要写下面两步代码:
1.读取图片数据,然后调用2中的方法进行校验
2.判断是否为有效的PNG图片,JPG的请使用UIImageJPEGRepresentation方法
真是眼泪都留下来了!~