2011-05-12 8 views
6

Estoy cargando una imagen clicada desde el iPhone en modo horizontal y vertical. La imagen con modo apaisado se carga bien, pero el problema es con la imagen cargada en modo retrato. Se giran 90 grados.imagen presionada desde el iPhone en el modo vertical se gira 90 grados

También otras imágenes con modo retrato (sin hacer clic desde el iPhone) funcionan bien.

¿Alguna idea de por qué sucede esto?

+0

donde estás subirlo a? – shabbirv

+0

Lo estoy cargando en mi servidor –

+0

En realidad, la imagen ya estaba girada, pero se mostró correctamente tanto en iPhone como en mac. –

Respuesta

5

En su delegado:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

Después de obtener su UIImage de "información" Inglés para la tecla "UIImagePickerControllerOriginalImage", Se puede ver la orientación de la imagen por la propiedad imageOrientation. Si no es lo que quiere, simplemente gire su imagen antes de subirla.

imageOrientation 
The orientation of the receiver’s image. (read-only) 

@property(nonatomic, readonly) UIImageOrientation imageOrientation 
Discussion 
Image orientation affects the way the image data is displayed when drawn. By default, images are displayed in the “up” orientation. If the image has associated metadata (such as EXIF information), however, this property contains the orientation indicated by that metadata. For a list of possible values for this property, see “UIImageOrientation.” 

Availability 
Available in iOS 2.0 and later. 
Declared In 
UIImage.h 

UIImage Class Reference

UIImagePickerController Class Reference

UIImagePickerControllerDelegate Protocol Reference

Segunda opción:

es permitir al usuario editar su imagen y obtener la imagen de "UIImagePickerControllerEditedImage";

Establezca su UIImagePickerController "allowsEditing" propiedad a Yes.

En su delegado, simplemente obtenga del diccionario "información" el UIImage para la clave "UIImagePickerControllerEditedImage".

Buena suerte.

+0

Eso es exactamente lo que he pensado. Pero mi problema es que todo funciona bien cuando no se hace clic en la imagen que se va a subir desde el iPhone. Pero solo si se hace clic a través de iPhone, se gira. Entonces, ¿el problema con la orientación de iPhone es que cuando convierte la imagen a nsdate, da los datos en forma rotativa? –

+0

Después de obtener la prueba de uiimage para la orientación de la imagen, gírela si tiene una orientación incorrecta y conviértala en nsdata. –

+1

El problema es que cuando cargo una imagen de retrato normal, funciona bien, pero cuando cargo la imagen de retrato capturada desde el iPhone, solo se gira. Además, si veo esa imagen en la máquina de Windows, ya está girada, pero cuando veo la misma imagen en Mac o iPhone, se ve bien. –

4

He luchado un poco con este problema, estaba trabajando en un proyecto en el que realmente necesito girar la imagen, como reorganizar los píxeles para poder subirlo.

Lo primero que debe hacer es determinar la orientación, luego quitar esos molestos metadatos y luego girar la imagen.

Así que poner esto en el interior de la función didFinishPickingMediaWithInfo:

UIImage * img = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 

    if ([info objectForKey:@"UIImagePickerControllerMediaMetadata"]) { 
    //Rotate based on orientation 
    switch ([[[info objectForKey:@"UIImagePickerControllerMediaMetadata"] objectForKey:@"Orientation"] intValue]) { 
     case 3: 
      //Rotate image to the left twice. 
      img = [UIImage imageWithCGImage:[img CGImage]]; //Strip off that pesky meta data! 
      img = [rotateImage rotateImage:[rotateImage rotateImage:img withRotationType:rotateLeft] withRotationType:rotateLeft]; 
      break; 

     case 6: 
      img = [UIImage imageWithCGImage:[img CGImage]]; 
      img = [rotateImage rotateImage:img withRotationType:rotateRight]; 
      break; 

     case 8: 
      img = [UIImage imageWithCGImage:[img CGImage]]; 
      img = [rotateImage rotateImage:img withRotationType:rotateLeft]; 
      break; 

     default: 
      break; 
    } 
} 

Y aquí es la función de cambio de tamaño:

+(UIImage*)rotateImage:(UIImage*)image withRotationType:(rotationType)rotation{ 
    CGImageRef imageRef = [image CGImage]; 
    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef); 
    CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB(); 

    if (alphaInfo == kCGImageAlphaNone) 
     alphaInfo = kCGImageAlphaNoneSkipLast; 

     CGContextRef bitmap; 

    bitmap = CGBitmapContextCreate(NULL, image.size.height, image.size.width, CGImageGetBitsPerComponent(imageRef), 4 * image.size.height/*CGImageGetBytesPerRow(imageRef)*/, colorSpaceInfo, alphaInfo); 
    CGColorSpaceRelease(colorSpaceInfo); 

    if (rotation == rotateLeft) { 
     CGContextTranslateCTM (bitmap, image.size.height, 0); 
     CGContextRotateCTM (bitmap, radians(90)); 
    } 
    else{ 
     CGContextTranslateCTM (bitmap, 0, image.size.width); 
     CGContextRotateCTM (bitmap, radians(-90)); 
    } 

    CGContextDrawImage(bitmap, CGRectMake(0, 0, image.size.width, image.size.height), imageRef); 
    CGImageRef ref = CGBitmapContextCreateImage(bitmap); 
    UIImage *result = [UIImage imageWithCGImage:ref]; 
    CGImageRelease(ref); 
    CGContextRelease(bitmap); 
    return result; 
} 

La variable img contiene ahora una imagen adecuada rotación.

0

Ok, una versión más limpia sería:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *img = [info valueForKey:UIImagePickerControllerOriginalImage]; 
    img = [UIImage imageWithCGImage:[img CGImage]]; 

    UIImageOrientation requiredOrientation = UIImageOrientationUp; 
    switch ([[[info objectForKey:@"UIImagePickerControllerMediaMetadata"] objectForKey:@"Orientation"] intValue]) 
    { 
     case 3: 
      requiredOrientation = UIImageOrientationDown; 
      break; 
     case 6: 
      requiredOrientation = UIImageOrientationRight; 
      break; 
     case 8: 
      requiredOrientation = UIImageOrientationLeft; 
      break; 
     default: 
      break; 
    } 

    UIImage *portraitImage = [[UIImage alloc] initWithCGImage:img.CGImage scale:1.0 orientation:requiredOrientation]; 

} 
Cuestiones relacionadas