在 UIGraphicsBeginImageContext 中绘制时 UIImage 方向正在改变 [英] UIImage orientation is changing when drawing in UIGraphicsBeginImageContext
问题描述
我想在 UIImage 上画一些线.我能够在上下文中绘制线条,最后我在使用以下代码从上下文中获取图像之前将背景图像绘制到它:
I want to draw some lines on the UIImage. I am able to draw the lines in to the Context and at the end I am drawing the background image to it before getting image from the context with the below code:
UIGraphicsBeginImageContext(self.bounds.size);
CGRect newFrame = self.frame;
newFrame.origin.y = 0;
[self.backgroundImage drawInRect:newFrame];
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
问题是:self.backgroundImage.imageOrientation 是 3 但 saveImage.imageOrientation 是 0.我怎样才能为 saveImage 获得相同的 imageOrientation?
problem is: self.backgroundImage.imageOrientation is 3 but saveImage.imageOrientation is 0. how can I get the same imageOrientation for the saveImage?
推荐答案
UIImage->drawInRect 将遵循图像方向,因此在该调用之后,结果始终为直立",因此您的 saveImage 实际上应该具有 imageOrientationUp.
UIImage->drawInRect will honor the image orientation, so after that call, the result is always "upright", therefore your saveImage should actually have imageOrientationUp.
如果您想在不影响图像的情况下更改方向元数据,可以使用如下所示的内容:
If you want to change the orientation metadata without affecting the image, you can use something like the following:
// Recreate UIImage with new orientation
- (UIImage *) imageWithOrientation:(UIImageOrientation)value
{
if (self.imageOrientation == value)
return self;
if (self.CGImage != nil)
return [UIImage imageWithCGImage:self.CGImage
scale:self.scale orientation:value];
if (self.CIImage != nil)
return [UIImage imageWithCIImage:self.CIImage
scale:self.scale orientation:value];
return nil;
}
这篇关于在 UIGraphicsBeginImageContext 中绘制时 UIImage 方向正在改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!