2012-02-13 7 views
9

Ricevo questo errore scaricando un'immagine tramite HTTP. Ho visto lo answer here ma anche le immagini valide non restituiscono YES dalla funzione.ImageIO: <ERROR> JPEG Dati JPEG danneggiati: fine prematura del segmento di dati iphone - come prendere questo?

Altre idee?

Il codice per ottenere l'immagine è abbastanza semplice. Questo succede in un thread in background.

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]; 
UIImage *image = [UIImage imageWithData:data]; 

Questa è la funzione da quel filo:

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

risposta

13

Utilizzare un unsigned char. Quindi il confronto dovrebbe funzionare.

const unsigned char * bytes = (const unsigned char *)[jpeg bytes]; 

invece di

const char * bytes = (const char *)[jpeg bytes]; 
+0

+1 grazie, Sigi! – Jean

+0

Sì, sembra funzionare. Grazie! – jmosesman

+0

Viene ancora visualizzato l'errore: : ImageIO: JPEG Dati JPEG corrotti: fine prematura del segmento di dati. :( –

Problemi correlati