2012-03-18 9 views
7

Sto afferrando alcune foto dalla libreria di foto di iPad. Ho scattato alcune foto di prova in modalità orizzontale, sia con il tasto home dell'iPad sul lato sinistro che sul lato destro.imageOrientation restituisce Su, ma la foto mostra upsideown

Quindi, nella mia app, per qualche motivo alcune delle foto vengono visualizzate di nuovo. Ho controllato la loro proprietà imageOrientation e sono tutti 0 (ovvero sono UIImageOrientationUp). Ciò significa che non posso nemmeno dire quali foto devo ruotare.

Cosa sta succedendo e come posso sapere quali foto devono essere ruotate? Grazie

+0

vedere questo https://github.com/cosnovae/fixUIImageOrientation –

+0

È inoltre possibile fare riferimento simile post [AVFoundation Orientamento immagine fuori di 90 gradi in anteprima, ma bene in rotolo Camera] (http://stackoverflow.com/questions/15956750/avfoundation-image-orientation-off-by-90-degrees-in-the-preview-but-fine-in-came/16074603#16074603) –

risposta

4

Un UIImage ha una proprietà imageOrientation, che indica a UIImageView e ad altri utenti UIImage di ruotare i dati delle immagini non elaborate. C'è una buona probabilità che questo flag venga salvato nei dati exif nell'immagine jpeg caricata, ma il programma che usi per visualizzarlo non sta rispettando quel flag.

Per ruotare l'UIImage per visualizzare correttamente quando caricato, è possibile utilizzare una categoria come questa: Nel file .h

UIImagefixOrientation.h

@interface UIImage (fixOrientation) 

- (UIImage *)fixOrientation; 

@end 

UIImagefixOrientation.m

@implementation UIImage (fixOrientation) 

- (UIImage *)fixOrientation { 

    // No-op if the orientation is already correct 
    if (self.imageOrientation == UIImageOrientationUp) return self; 

    // We need to calculate the proper transformation to make the image upright. 
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored. 
    CGAffineTransform transform = CGAffineTransformIdentity; 

    switch (self.imageOrientation) { 
     case UIImageOrientationDown: 
     case UIImageOrientationDownMirrored: 
     transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height); 
     transform = CGAffineTransformRotate(transform, M_PI); 
      break; 

    case UIImageOrientationLeft: 
    case UIImageOrientationLeftMirrored: 
     transform = CGAffineTransformTranslate(transform, self.size.width, 0); 
     transform = CGAffineTransformRotate(transform, M_PI_2); 
     break; 

    case UIImageOrientationRight: 
    case UIImageOrientationRightMirrored: 
     transform = CGAffineTransformTranslate(transform, 0, self.size.height); 
     transform = CGAffineTransformRotate(transform, -M_PI_2); 
     break; 
} 

switch (self.imageOrientation) { 
    case UIImageOrientationUpMirrored: 
    case UIImageOrientationDownMirrored: 
     transform = CGAffineTransformTranslate(transform, self.size.width, 0); 
     transform = CGAffineTransformScale(transform, -1, 1); 
     break; 

    case UIImageOrientationLeftMirrored: 
    case UIImageOrientationRightMirrored: 
     transform = CGAffineTransformTranslate(transform, self.size.height, 0); 
     transform = CGAffineTransformScale(transform, -1, 1); 
     break; 
} 

// Now we draw the underlying CGImage into a new context, applying the transform 
// calculated above. 
CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height, 
             CGImageGetBitsPerComponent(self.CGImage), 0, 
             CGImageGetColorSpace(self.CGImage), 
             CGImageGetBitmapInfo(self.CGImage)); 
CGContextConcatCTM(ctx, transform); 
switch (self.imageOrientation) { 
    case UIImageOrientationLeft: 
    case UIImageOrientationLeftMirrored: 
    case UIImageOrientationRight: 
    case UIImageOrientationRightMirrored: 
     // Grr... 
     CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage); 
     break; 

    default: 
     CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage); 
     break; 
} 

// And now we just create a new UIImage from the drawing context 
CGImageRef cgimg = CGBitmapContextCreateImage(ctx); 
UIImage *img = [UIImage imageWithCGImage:cgimg]; 
CGContextRelease(ctx); 
CGImageRelease(cgimg); 
return img; 
} 

@end 

E chiamare questa funzione durante il salvataggio dell'immagine di cattura o selezionare l'immagine dalla galleria.

+1

Ciao, grazie per il tuo funzione. Tuttavia, un potenziale problema è che i file capovolti restituiscono UIImageOrientationUp. Nessuno di essi restituisce qualcosa di diverso da 0, il che è problematico. – user339946

+0

puoi mandarmi il codice perché lo proverò dalla mia parte.e ho risolto un errore. – parag

1

Senza entrare nel codice dell'immagine, potrebbe essere dovuto al sistema di coordinate capovolte su iOS?

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Transforms/Transforms.html

~ snip:

Configurare l'immagine per utilizzare Flipped Coordinate

Il primo passo è necessario prendere per implementare le coordinate capovolto è quello di decidere l'orientamento predefinito del vostro vista. Se preferisci utilizzare le coordinate capovolte, ci sono due modi per configurare il sistema di coordinate della vista prima del disegno:

Ignora il metodo isFlipped della tua vista e ritorna SÌ. Applicare un flip trasformare al tuo contenuto immediatamente prima del rendering.

Quindi, a suo avviso, si può provare ad aggiungere -(BOOL)isFlipped e tornare YES e verificare se questo fa alcuna differenza.

1

Anche io stavo affrontando lo stesso problema. utilizzare questa funzione per risolvere:

- (UIImage *)scaleAndRotateImage:(UIImage *) image { 
int kMaxResolution = 320; 

CGImageRef imgRef = image.CGImage; 

CGFloat width = CGImageGetWidth(imgRef); 
CGFloat height = CGImageGetHeight(imgRef); 


CGAffineTransform transform = CGAffineTransformIdentity; 
CGRect bounds = CGRectMake(0, 0, width, height); 
if (width > kMaxResolution || height > kMaxResolution) { 
    CGFloat ratio = width/height; 
    if (ratio > 1) { 
     bounds.size.width = kMaxResolution; 
     bounds.size.height = bounds.size.width/ratio; 
    } 
    else { 
     bounds.size.height = kMaxResolution; 
     bounds.size.width = bounds.size.height * ratio; 
    } 
} 

CGFloat scaleRatio = bounds.size.width/width; 
CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); 
CGFloat boundHeight; 
UIImageOrientation orient = image.imageOrientation; 
switch(orient) { 

    case UIImageOrientationUp: //EXIF = 1 
     transform = CGAffineTransformIdentity; 
     break; 

    case UIImageOrientationUpMirrored: //EXIF = 2 
     transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0); 
     transform = CGAffineTransformScale(transform, -1.0, 1.0); 
     break; 

    case UIImageOrientationDown: //EXIF = 3 
     transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height); 
     transform = CGAffineTransformRotate(transform, M_PI); 
     break; 

    case UIImageOrientationDownMirrored: //EXIF = 4 
     transform = CGAffineTransformMakeTranslation(0.0, imageSize.height); 
     transform = CGAffineTransformScale(transform, 1.0, -1.0); 
     break; 

    case UIImageOrientationLeftMirrored: //EXIF = 5 
     boundHeight = bounds.size.height; 
     bounds.size.height = bounds.size.width; 
     bounds.size.width = boundHeight; 
     transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width); 
     transform = CGAffineTransformScale(transform, -1.0, 1.0); 
     transform = CGAffineTransformRotate(transform, 3.0 * M_PI/2.0); 
     break; 

    case UIImageOrientationLeft: //EXIF = 6 
     boundHeight = bounds.size.height; 
     bounds.size.height = bounds.size.width; 
     bounds.size.width = boundHeight; 
     transform = CGAffineTransformMakeTranslation(0.0, imageSize.width); 
     transform = CGAffineTransformRotate(transform, 3.0 * M_PI/2.0); 
     break; 

    case UIImageOrientationRightMirrored: //EXIF = 7 
     boundHeight = bounds.size.height; 
     bounds.size.height = bounds.size.width; 
     bounds.size.width = boundHeight; 
     transform = CGAffineTransformMakeScale(-1.0, 1.0); 
     transform = CGAffineTransformRotate(transform, M_PI/2.0); 
     break; 

    case UIImageOrientationRight: //EXIF = 8 
     boundHeight = bounds.size.height; 
     bounds.size.height = bounds.size.width; 
     bounds.size.width = boundHeight; 
     transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0); 
     transform = CGAffineTransformRotate(transform, M_PI/2.0); 
     break; 

    default: 
     [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"]; 

} 

UIGraphicsBeginImageContext(bounds.size); 

CGContextRef context = UIGraphicsGetCurrentContext(); 

if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) { 
    CGContextScaleCTM(context, -scaleRatio, scaleRatio); 
    CGContextTranslateCTM(context, -height, 0); 
} 
else { 
    CGContextScaleCTM(context, scaleRatio, -scaleRatio); 
    CGContextTranslateCTM(context, 0, -height); 
} 

CGContextConcatCTM(context, transform); 

CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef); 
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

return imageCopy; 
} 
Problemi correlati