Cgcontextdrawimage Draws Image Upside Down When Passed Uiimage.Cgimage

CGContextDrawImage draws image upside down when passed UIImage.CGImage

Instead of

CGContextDrawImage(context, CGRectMake(0, 0, 145, 15), image.CGImage);

Use

[image drawInRect:CGRectMake(0, 0, 145, 15)];

In the middle of your begin/end CGcontext methods.

This will draw the image with the correct orientation into your current image context - I'm pretty sure this has something to do with the UIImage holding onto knowledge of the orientation while the CGContextDrawImage method gets the underlying raw image data with no understanding of orientation.

iPhone - Image drawn upside down into a CGGraphic context (custom UIView)

This is because Core Graphics is using a flipped coordinate system.

You could try flipping the object you're drawing to using the isFlipped property.

Or you could try using this code in your drawing method (Source):

CGContextTranslateCTM(context, 0, image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

Using CGContext of UIGraphicImageRenderer to redraw an image, flipped the image. How to prevent the flip?

No need to get the cgImage. you can simply draw the UIImage:

extension UIImage {
func toSRGB() -> UIImage {
UIGraphicsImageRenderer(size: size).image { _ in
draw(in: CGRect(origin: .zero, size: size))
}
}
}

The reason why your image got flipped is the difference between coordinate system in UIKit and Quartz. UIKit vertical origin is at the top while Quartz vertical origin is at the bottom. The easiest way to fix this is to apply a CGAffineTransform scaling it by minus one and then translating the height distance backwards:

extension CGAffineTransform {
static func flippingVerticaly(_ height: CGFloat) -> CGAffineTransform {
var transform = CGAffineTransform(scaleX: 1, y: -1)
transform = transform.translatedBy(x: 0, y: -height)
return transform
}
}


extension UIImage {
func toSRGB() -> UIImage {
guard let cgImage = cgImage else { return self }
return UIGraphicsImageRenderer(size: size).image { ctx in
ctx.cgContext.concatenate(.flippingVerticaly(size.height))
ctx.cgContext.draw(cgImage, in: CGRect(origin: .zero, size: size))
}
}
}

edit/update:

To keep the scale at 1x you can pass an image renderer format:

extension UIImage {
func toSRGB() -> UIImage {
guard let cgImage = cgImage else { return self }
let format = imageRendererFormat
format.scale = 1
return UIGraphicsImageRenderer(size: size, format: format).image { ctx in
ctx.cgContext.concatenate(.flippingVerticaly(size.height))
ctx.cgContext.draw(cgImage, in: CGRect(origin: .zero, size: size))
}
}
}

Why is CGContextDrawImage rotating my image

It's a very easy question.

Because iOS system has 3 different Coordinate System.

UIKit - y axis direction is down
Core Graphics(Quartz) - y axis direction is up
OpenGL ES - y axis direction is up

The originalImage is create by UIKit and the target image is create by Core Graphics, so it should be upside-down.

If you want get the correct image, you can use:

UIImageView* imageV = [[UIImageView alloc] initWithFrame:CGRectMake(100,100, 100, 100)];
imageV.layer.borderColor = [UIColor redColor].CGColor;
imageV.layer.borderWidth = 1;
imageV.image = img;
imageV.layer.transform = CATransform3DRotate(imageV.layer.transform, M_PI, 1.0f, 0.0f, 0.0f);
[self.view addSubview:imageV];

Hope it can help you.

Why is my image upside down after using CGContextSetFillColorWithColor

CGContext coordinate system is flipped vertically in regard to UIView coordinate system.
Just flip it like this:
CGContextTranslateCTM(ctx, 0, imageHeight);,
CGContextScaleCTM(ctx, 1, -1);

iPhone MapKit: Custom MKPolygonView with image draws upside down

Fixed! This is the code that gets the job done:

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
UIImage *image = [UIImage imageNamed:@"snowmap.png"];
MKMapRect theMapRect = [self.overlay boundingMapRect];
CGRect theRect = [self rectForMapRect:theMapRect];

UIGraphicsPushContext(context);
[image drawInRect:theRect blendMode:kCGBlendModeNormal alpha:1.0];
UIGraphicsPopContext();
}

CGImage in CALayer appears upside down

Eventually i just rotate the UIImage before converting it to CGImgae.
This cane also be achieved by rotating the layer itself using affineTransform().

CGContextDrawImage in Retina draws image pixelated?

You need to set the contents scale of the layer appropriately.

myLayer.contentsScale = [UIScreen mainScreen].scale

UIImage turns upside down when rescaled - sometimes

Okay, found a workaround: Create the thumbnail first, then fix (if necessary) the original image and the thumbnail's orientation (by copying them):

// Create thumbnail:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(94, 76), true, 0.0)

image.drawInRect(CGRectMake(0, 0, 94, 76))

var thumbnailImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()


// Fix orientation:

switch image.imageOrientation {
case .Up, .UpMirrored:
break

default:
image = UIImage(CGImage: image.CGImage!, scale: 1.0, orientation: UIImageOrientation.Up)
}


let imageData = UIImagePNGRepresentation(image)


switch thumbnailImage.imageOrientation {
case .Up, .UpMirrored:
break

default:
thumbnailImage = UIImage(CGImage: thumbnailImage.CGImage!, scale: 1.0, orientation: UIImageOrientation.Up)
}

// (...use both images...)

Still, like before, the thumbnail re-orienting case is never executed (thumbnail orientation always equals .Up from the beginning).

I really don't understand what's going on here, but perhaps some image data is being referenced somewhere instead of being copied, and that causes inconsistencies. I must be missing some detail in the specification...

IOS UIImage Image upside down

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);


Related Topics



Leave a reply



Submit