IOS UIImage image upside down

If I draw my image, I fixed the problem using CGAffintrasform

CGAffineTransform myTr = CGAffineTransformMake(1, 0, 0, -1, 0, backImage.size.height); CGContextConcatCTM(context, myTr); [backImage drawInRect:CGRectMake(cbx, -cby, backImage.size.width, backImage.size.height)]; myTr = CGAffineTransformMake(1, 0, 0, -1, 0, backImage.size.height); CGContextConcatCTM(context, myTr); 

when I want to write to a file I use this

  NSData *imageData = UIImageJPEGRepresentation(backImage, 0); 

then the image is upside down how?

+6
source share
2 answers

If you want to get UIImage to save use:

 UIGraphicsBeginImageContextWithOptions(size, isOpaque, 0); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextDrawImage(context, (CGRect){ {0,0}, origSize }, [origImage CGImage]); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; 

Then do:

  NSData *imageData = UIImageJPEGRepresentation(backImage, 0); 
+2
source

First create an identity matrix.

 1, 0, 0 0, 1, 0 0, 0, 1 CGAffineTransform matrix = CGAffineTransformMake(1, 0, 0, 1, 0, 0); 

Move position to draw ...

 matrix = CGAffineTransformTranslate(matrix, x, y); 

The flip matrix is ​​horizontal.

 matrix = CGAffineTransformScale(matrix, -1, 1); 

Flip matrix is ​​vertical.

 matrix = CGAffineTransformScale(matrix, 1, -1); 

Rotate matrix

 matrix = CGAffineTransformRotate(matrix, angle); 

Set UIImage to UIView.

 matrix = CGAffineTransformScale(matrix, imageWidth/viewWidth, imageheight/viewHeight); 

Run matrix in context.

 CGContextConcatCTM(context, matrix); 

Draw a picture.

 [backImage drawAtPoint:CGPointMake(0, 0)]; 

:)

+2
source

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


All Articles