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

谢谢你,盖伊。

4

1 回答 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
于 2012-04-23T14:42:13.643 回答