ImageIO:<错误>JPEG 损坏的 JPEG 数据:iphone 数据段过早结束 -

2023-10-23移动开发问题
5

本文介绍了ImageIO:<错误>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];

这是来自该线程的函数:

This is the function from that thread:

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

推荐答案

使用无符号字符.然后比较应该可以工作.

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;错误&gt;JPEG 损坏的 JPEG 数据:iphone 数据段过早结束 - 如何捕捉到这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

Cocos2d - 如何检查不同层中对象之间的交集
Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)...
2024-08-12 移动开发问题
8

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

突出显示朗读文本(在 iPhone 的故事书类型应用程序中)
Highlight Read-Along Text (in a storybook type app for iPhone)(突出显示朗读文本(在 iPhone 的故事书类型应用程序中))...
2024-08-12 移动开发问题
9

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

正确的 cocos2d 场景重启?
Proper cocos2d scene restart?(正确的 cocos2d 场景重启?)...
2024-08-12 移动开发问题
7