2013-10-31 22 views
1

我需要以下代码的一些帮助。我现在已经设置了这样一个图像,使图像从黑色变成白色,然后在每次点击时从白色变回黑色。问题在于它不仅仅是翻转颜色,它实际上也是颠倒了图像。我如何让它停止垂直翻转图像?有在那里没有方位元素,所以我一直搞不清楚什么部分是使它做到这一点:iOS从翻转图像停止?

#define FLIP_BUTTON_TAG 200 
#define FLIP_VUTTON_TAG2 300 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    flipBtn.tag = FLIP_BUTTON_TAG; 
} 

    UIImage* flippedImage; 
     if(flipBtn.tag==FLIP_BUTTON_TAG) 
     { 

      // set the fill color 

      UIImage *sourceImage1 = self.outletImageView.image; 
      CGRect rect = CGRectMake(0, 0, sourceImage1.size.width, sourceImage1.size.height); 
      UIGraphicsBeginImageContext(rect.size); 
      CGContextRef context = UIGraphicsGetCurrentContext(); 
      CGContextClipToMask(context, rect, sourceImage1.CGImage); 
      CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]); 
      CGContextFillRect(context, rect); 
      sourceImage1 = UIGraphicsGetImageFromCurrentImageContext(); 
      UIGraphicsEndImageContext(); 
      flippedImage = sourceImage1; 

      flipBtn.tag = FLIP_VUTTON_TAG2; 

    }else{ 

     UIImage* sourceImage1 = self.outletImageView.image; 
     CGRect rect = CGRectMake(0, 0, sourceImage1.size.width, sourceImage1.size.height); 
     UIGraphicsBeginImageContext(rect.size); 
     CGContextRef context = UIGraphicsGetCurrentContext(); 
     CGContextClipToMask(context, rect, sourceImage1.CGImage); 
     CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]); 
     CGContextFillRect(context, rect); 
     sourceImage1 = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     flippedImage = sourceImage1; 


     flipBtn.tag = FLIP_BUTTON_TAG; 

    } 

    NSInteger index1 = [imgViewArray indexOfObject:self.outletImageView]; 
    [imgViewArray removeObject:self.outletImageView]; 
    self.outletImageView.image = flippedImage; 
    [imgViewArray insertObject:self.outletImageView atIndex:index1]; 

} 

回答

3

这可能是因为CGContext上具有不同的坐标系,试试你的绘图代码之前加入这个(CGContextClipToMask前) :

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

如果我没有记错CGContext上必须在与Y正走上坡路右下角的起源,而UIKit中使用相反(在右上方的起源,Y去向下)。

+0

这样做!非常感谢! – dfox