2012-07-29 13 views
6

cargo la imagen desde el rollo de la cámara, pero esta imagen está boca abajo. entonces escribí método para rotarlo.Giro la imagen al revés pero cómo voltearla horizontalmente

CGImageRef imageRef = [image CGImage]; 

float width = CGImageGetWidth(imageRef); 
float height = CGImageGetHeight(imageRef); 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
Byte *rawData = malloc(height * width * 4); 
Byte bytesPerPixel = 4; 
int bytesPerRow = bytesPerPixel * width; 
Byte bitsPerComponent = 8; 
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
CGColorSpaceRelease(colorSpace); 
int byteIndex = 0; 
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); 

Byte *rawData2 = malloc(height * width * 4); 

for (int i = 0 ; i < width * height ; i++) { 
    int index = (width * height) * 4; 
    rawData2[byteIndex + 0] = rawData[index - byteIndex + 0]; 
    rawData2[byteIndex + 1] = rawData[index - byteIndex + 1]; 
    rawData2[byteIndex + 2] = rawData[index - byteIndex + 2]; 
    rawData2[byteIndex + 3] = rawData[index - byteIndex + 3]; 

    byteIndex += 4; 
} 

CGContextRef ctx = CGBitmapContextCreate(rawData2, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef), 8, CGImageGetBytesPerRow(imageRef), CGImageGetColorSpace(imageRef),kCGImageAlphaPremultipliedLast); 

imageRef = CGBitmapContextCreateImage (ctx); 
image = [UIImage imageWithCGImage:imageRef]; 

CGContextRelease(context); 

return image; 

está bien, pero ahora debo voltearlo horizontalmente, y no sé cómo puedo hacer esto. Intento hacer este segundo día.

gracias por la ayuda

Respuesta

21

¿Has probado esto:

imageView.transform = CGAffineTransformMakeScale(-1, 1); 

?

También se puede hacer la rotación mediante el uso de una transformación:

imageView.transform = CGAffineTransformMakeRotation(M_PI); 

Usted puede concat los dos transformación en uno como este:

imageView.transform = CGAffineTransformRotation(CGAffineTransformMakeScale(-1, 1), M_PI); 

Si desea hacer su propia UIImage objeto, en lugar de manipular vistas y transformaciones, le sugiero que use el enfoque descrito anteriormente para hacer que la vista dibuje la imagen como desee, luego convierta su contenido UIView en un objeto UIImage:

UIGraphicsBeginImageContext(rect.size); 
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+0

debo transformar UIImage y dibujarlo en CGContextRef, por lo que '.transform' no está disponible. Intento agregar UIImage a UIImageView, rotarlo y hacer 'image = imageView.image', pero no está funcionando. –

+0

Por favor, vea mi edición sobre cómo puede "convertir" el contenido de la vista en una imagen. – sergio

+0

está funcionando pero configuro 'imageview.frame = CGRectMake (111,122,768,1024)' y muestra el contexto en '0,0,768,1024'. ¿cualquier sugerencia? –

Cuestiones relacionadas