I have a image similar to the following:
I guess this image may not be completely downloaded.
How to detect this image?
I've tried the following code, but it does not work.
-(BOOL)dataIsValidJPEG:(NSData *)data
{
if (!data || data.length < 2) return NO;
NSInteger totalBytes = data.length;
const char *bytes = (const char*)[data bytes];
return (bytes[0] == (char)0xff &&
bytes[1] == (char)0xd8 &&
bytes[totalBytes-2] == (char)0xff &&
bytes[totalBytes-1] == (char)0xd9);
}
- (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;
}