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.
donde estás subirlo a? – shabbirv
Lo estoy cargando en mi servidor –
En realidad, la imagen ya estaba girada, pero se mostró correctamente tanto en iPhone como en mac. –