voltear que online jpg imagen espejo convertir archivos archivo abrir aae iphone objective-c ios core-graphics

iphone - online - que es un archivo aae



Girando un contexto de imagen(CGContextRotateCTM) causando que quede en blanco. ¿Por qué? (5)

Ampliando la respuesta de Nielsbot, creé el siguiente fragmento. Tenga en cuenta que, preferiblemente, esto debería hacerse una función en lugar de un fragmento de código, para evitar la "programación copiar / pegar". Todavía no llegué a eso, así que siéntete libre de adaptarlo.

Fragmento de código:

// <Draw in frame with orientation> UIImage* imageToDrawRotated = <#image#>; float rotationAngle = <#angle#>; CGRect drawFrame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>); CGContextTranslateCTM(UIGraphicsGetCurrentContext(), floor(drawFrame.origin.x + drawFrame.size.width/2), floor(drawFrame.origin.y + drawFrame.size.height/2)); CGContextRotateCTM(UIGraphicsGetCurrentContext(), rotationAngle); [imageToDrawRotated drawInRect:CGRectMake(-floor(drawFrame.size.width/2), -floor(drawFrame.size.height/2), drawFrame.size.width, drawFrame.size.height)]; CGContextRotateCTM(UIGraphicsGetCurrentContext(), -rotationAngle); CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -floor(drawFrame.origin.x + drawFrame.size.width/2), -floor(drawFrame.origin.y + drawFrame.size.height/2)); // </Draw in frame with orientation>

Además, doy la bienvenida a una mejor manera de restablecer el CGContext a su estado anterior.

#define radians(degrees) (degrees * M_PI/180) UIImage *rotate(UIImage *image) { CGSize size = image.size;; UIGraphicsBeginImageContext(size); CGContextRef context = UIGraphicsGetCurrentContext(); // If this is commented out, image is returned as it is. CGContextRotateCTM (context, radians(90)); [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }

¿Podría algo más estar mal? Sin ideas.


El problema es que su contexto se está girando alrededor de (0,0), que es la esquina superior izquierda. Si gira 90 grados alrededor de la esquina superior izquierda, todo el dibujo se producirá fuera de los límites del contexto. Necesita una transformación de 2 pasos para mover el origen del contexto a su punto medio, y luego GIRAR. También debe dibujar su imagen centrada en el origen movido / girado, como este:

CGContextTranslateCTM( context, 0.5f * size.width, 0.5f * size.height ) ; CGContextRotateCTM( context, radians( 90 ) ) ; [ image drawInRect:(CGRect){ { -size.width * 0.5f, -size.height * 0.5f }, size } ] ;

HTH


Intento seguir el código, funciona, obtener de catamount.com/blog/1015/…

+ (UIImage *)rotateImage:(UIImage*)src byRadian:(CGFloat)radian { // calculate the size of the rotated view''s containing box for our drawing space //UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0, src.size.width, src.size.height)]; //CGAffineTransform t = CGAffineTransformMakeRotation(radian); //rotatedViewBox.transform = t; //CGSize rotatedSize = rotatedViewBox.frame.size; CGRect rect = CGRectApplyAffineTransform(CGRectMake(0,0, src.size.width, src.size.height), CGAffineTransformMakeRotation(radian)); CGSize rotatedSize = rect.size; // Create the bitmap context UIGraphicsBeginImageContext(rotatedSize); CGContextRef bitmap = UIGraphicsGetCurrentContext(); // Move the origin to the middle of the image so we will rotate and scale around the center. CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); // // Rotate the image context CGContextRotateCTM(bitmap, radian); // Now, draw the rotated/scaled image into the context CGContextScaleCTM(bitmap, 1.0, -1.0); CGContextDrawImage(bitmap, CGRectMake(-src.size.width / 2, -src.size.height / 2, src.size.width, src.size.height), [src CGImage]); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }


Me gustó la solución de @lbsweek, pero estaba un poco preocupado por la asignación de UIView solo para la transformación, en su lugar, probé a continuación

- (UIImage *)rotateImage:(UIImage *)sourceImage byDegrees:(float)degrees { CGFloat radian = degrees * (M_PI/ 180); CGRect contextRect = CGRectMake(0, 0, sourceImage.size.width, sourceImage.size.height); float newSide = MAX([sourceImage size].width, [sourceImage size].height); CGSize newSize = CGSizeMake(newSide, newSide); UIGraphicsBeginImageContext(newSize); CGContextRef ctx = UIGraphicsGetCurrentContext(); CGPoint contextCenter = CGPointMake(CGRectGetMidX(contextRect), CGRectGetMidY(contextRect)); CGContextTranslateCTM(ctx, contextCenter.x, contextCenter.y); CGContextRotateCTM(ctx, radian); CGContextTranslateCTM(ctx, -contextCenter.x, -contextCenter.y); [sourceImage drawInRect:contextRect]; UIImage* rotatedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return rotatedImage; }


Si no quiere cambiar todos los orígenes y tamaños, mueva el centro, gire y cambie el centro a la esquina nuevamente de esta manera:

CGContextTranslateCTM( context, 0.5f * size.width, 0.5f * size.height ) ; CGContextRotateCTM( context, radians( 90 ) ) ; CGContextTranslateCTM( context, -0.5f * size.width, -0.5f * size.height ) ;

Luego dibuja normalmente así:

[image drawInRect:CGRectMake(0, 0, size.width, size.height)];