I turn the image upside down, but how to flip it horizontally

Loading image from camera, but this image is upside down. so I wrote a method to rotate it.

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; 

this is normal, but now I have to flip it horizontally, and I don’t know how to do it. I'm trying to make this second day.

thank you for your help

+6
source share
1 answer

Have you tried this:

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

?

You can also perform a rotation using conversion:

 imageView.transform = CGAffineTransformMakeRotation(M_PI); 

You can perform two conversions in one, like this:

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

If you want to create your own UIImage object, rather than manipulate views and transformations, I would still suggest you use the approach described above to make the image painted as you like, and then convert the UIView content to a UIImage object:

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

Source: https://habr.com/ru/post/921597/


All Articles