2009-06-25 6 views
8

sto usando UIImagePickerController per scattare una foto in modalità ritratto su iPhone e salvare al web. La foto viene visualizzata in verticale sul telefono, ma ruota di 90 gradi sul web.le immagini della fotocamera di iPhone vengono ruotate Dopo essere stati caricati web

Se scarico la foto e la guardo in Anteprima (mac) o Photoshop (mac o pc) è di nuovo in verticale. In Windows Picture Viewer (PC) viene ruotato in orizzontale.

ho bisogno di applicare una rotazione trasformare i dati di immagine prima del caricamento? Dovrò quindi anche rimuovere i meta-dati che lo ruotano in Photoshop e in Anteprima?

risposta

7

Il problema era che la rotazione delle immagini è stato aggiunto alla foto come dati EXIF ​​non utilizzato dalla maggior parte dei browser. Esistono due soluzioni:

  1. Applicare la rotazione sul lato server. Stavo usando il plugin Ruby Paperclip (di Thoughtbot) e dovevo semplicemente includere l'opzione di conversione auto-orient nel comando has_attached_file nel modello:

    has_attached_file: photo,: convert_options => {: all => '-auto -orient '}

  2. Ruota la foto all'interno dell'app per iPhone. Questo è stato risolto in un'altra domanda StackOverflow; chiamare lo scaleAndRotate method sostituisce i meta-dati di rotazione con una trasformazione dell'immagine, grazie a @Squeegy.

+0

# 2 funziona bene – byron

0

volta che si prende la foto prima di caricare l'immagine al server appena passare la vostra immagine presa in un metodo e la sua sicuramente funziona per voi

#pragma mark Rotate 
- (UIImage *)scaleAndRotateImage:(UIImage *)image { 
    int kMaxResolution = 640; // Or whatever 

    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 = roundf(bounds.size.width/ratio); 
     } 
     else { 
      bounds.size.height = kMaxResolution; 
      bounds.size.width = roundf(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; 
} 
+0

Credo che i tuoi commenti non siano corretti. Per esempio. UIImageOrientationUp: // EXIF ​​= 0 – jonypz

-1

Ecco un modo semplice per ignorare manualmente i metadati di rotazione EXIF, se l'immagine viene salvata con l'orientamento corretto in MS Windows. In Esplora risorse, fare clic con il tasto destro del mouse sul file immagine e selezionare "Ruota in senso orario". Fai questo 4 volte per ruotare l'immagine completamente intorno, e quindi l'immagine avrà l'orientamento corretto per tutti i sistemi. Quindi puoi caricare l'immagine sul tuo server web.