1

I have a image similar to the following: enter image description here

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;
}
Vincent Sit
  • 2,214
  • 1
  • 24
  • 27
  • 1
    try this answer http://stackoverflow.com/a/9990940/2028997 – Shmatlay Andrey Sep 16 '14 at 10:55
  • @ShmatlayAndrey :] Thanks for your advice, but still not work... – Vincent Sit Sep 16 '14 at 11:12
  • @Vincent have you found solution to this? I am encountered same problem. But I am getting `JPEGDecompressSurface : Picture decode failed:` exception. And If I were to capture this exception will solve the issue. – Yoon Lee Jun 29 '17 at 18:22
  • @YoonLee No solution was found...... – Vincent Sit Jun 30 '17 at 01:37
  • 1
    @Vincent After run many number of times research, I finally conclude compare the Network response header(Content-Length) and physical file length by `[[NSFileManager defaultManager] attributesOfItemAtPath:tmpImgPath error:&possibleFileError]` -> `NSDictionary *attributes`'s `fileSize` solved issue. Course, those size value are `unsigned long long` type. You can verify the image corrupted by terminal `ImageMagick` tool's `identify -verbose fileName` to see detail. – Yoon Lee Jun 30 '17 at 17:30
  • 1
    Also, there is `C Language` base `ImageMagick` library, but didn't have much time to port into my current project. – Yoon Lee Jun 30 '17 at 17:33

0 Answers0