我尝试翻转(镜像)UIImage的方式有什么问题?

5
我已经尝试了几天。我正在创建一个精灵表单加载器,但我也必须能够加载面向相反方向的精灵。这需要翻转已经加载的图像。
我已经尝试使用UIImageOrientation/UIImageOrientationUpMirrored方法来实现这一点,但是这完全没有效果,只是以与之前完全相同的方向绘制帧。
我随后尝试了一种稍微复杂一些的方法,我将在下面包含。但是,它仍然只是以与应用程序加载的方式完全相同的方式绘制图像(未镜像)。
我在下面包含了该方法(以及我的注释,以便您可以跟随我的思维模式),您能看出我做错了什么吗?
- (UIImage*) getFlippedFrame:(UIImage*) imageToFlip
{
//create a context to draw that shizz into
UIGraphicsBeginImageContext(imageToFlip.size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();



//WHERE YOU LEFT OFF. you're attempting to find a way to flip the image in imagetoflip. and return it as a new UIimage. But no luck so far.
[imageToFlip drawInRect:CGRectMake(0, 0, imageToFlip.size.width, imageToFlip.size.height)];

//take the current context with the old frame drawn in and flip it.
CGContextScaleCTM(currentContext, -1.0, 1.0);

//create a UIImage made from the flipped context. However will the transformation survive the transition to UIImage? UPDATE: Apparently not.

UIImage* flippedFrame = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return flippedFrame;
}

谢谢,Guy。
1个回答

6

我原本认为您需要更改上下文的转换,然后进行绘制。此外,由于您需要翻转到负坐标,因此还需要进行平移操作,所以请将以下内容替换:

[imageToFlip drawInRect:CGRectMake(0, 0, imageToFlip.size.width, imageToFlip.size.height)];
CGContextScaleCTM(currentContext, -1.0, 1.0);

基于评论进行了编辑

CGContextTranslateCTM(currentContext, imageToFlip.size.width, 0);      
CGContextScaleCTM(currentContext, -1.0, 1.0);
[imageToFlip drawInRect:CGRectMake(0, 0, imageToFlip.size.width, imageToFlip.size.height)];

注意:从评论中获取可用分类。
@implementation UIImage (Flip) 
  - (UIImage*)horizontalFlip { 
     UIGraphicsBeginImageContext(self.size); 
     CGContextRef current_context = UIGraphicsGetCurrentContext();                           
     CGContextTranslateCTM(current_context, self.size.width, 0);
     CGContextScaleCTM(current_context, -1.0, 1.0); 
     [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)]; 
     UIImage *flipped_img = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     return flipped_img; 
  } 
@end

如果您对核心图形有所了解,能否告诉我为什么我们要先转换上下文再进行绘制呢?我本以为应该先在上下文中绘制,然后再将上下文转换以使它们一起被转换,但显然这是不正确的。 - Guy Joel McLean
我不知道我能否回答为什么,但其中一个优点是你可以进行变换、绘制、再次变换、绘制等操作。你可能想这样做以获得不同的效果。 - Lou Franco
BOOM!原来CG非常类似于OpenGL的glTranslate、glRotate和glScale调用。你必须按照与所要执行的操作相反的顺序调用它们。例如,在这里,我们希望将上下文在x方向上缩放-1,然后将其平移到ImageToFlip的宽度。但是在实践中,我们编写:CGContextTranslateCTM(currentContext,imageToFlip.size.width,0); CGContextScaleCTM(currentContext,-1.0,1.0);以前我无意中正在转换上下文,然后才缩放其中的内容(没有内容)。 - Guy Joel McLean
2
非常好用!这是基于您的代码的UIImage类别:`@implementation UIImage (Flip)
  • (UIImage*)horizontalFlip { UIGraphicsBeginImageContext(self.size); CGContextRef current_context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(current_context, self.size.width, 0); CGContextScaleCTM(current_context, -1.0, 1.0); [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)]; UIImage *flipped_img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return flipped_img;
}@end`
- digipeople
请用您的代码和之前和之后的图像创建一个新问题。 - Lou Franco
显示剩余8条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接