IOS UIImage Image upside down

2019-04-07 19:16发布

问题:

if i draw my image I fixed the problem use 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 wanna write to file i use this

 NSData *imageData = UIImageJPEGRepresentation(backImage, 0);  

then Image upside down how ?

回答1:

When you want to get a 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:

First make Identity matrix.

1, 0, 0
0, 1, 0
0, 0, 1

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

Move position for drawing...

matrix = CGAffineTransformTranslate(matrix, x, y);

Flip matrix horizontal.

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

Flip matrix vertical.

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

Rotate matrix

matrix = CGAffineTransformRotate(matrix, angle);

Fit scale UIImage to UIView.

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

Launch matrix into the context.

CGContextConcatCTM(context, matrix);

Draw image.

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

:)