2014-04-17 29 views
0

我试图检查图像的当前方向并将其翻转到与当前的相反。iOS:如何根据当前方向翻转图像?

如果我的屏幕上只有一个元素,此代码有效,但只要我增加了阵列的50/50获取方向权限的机会。

-(IBAction)flipBtn:(id)sender 
{ 

    UIImage* flippedImage; 
    if(flipBtn.tag==FLIP_BUTTON_TAG) 
    { 
     UIImage* sourceImage1 = self.outletImageView.image; 
     flippedImage = [UIImage imageWithCGImage:sourceImage1.CGImage scale:1.0 orientation: UIImageOrientationUpMirrored]; 
     flipBtn.tag = FLIP_VUTTON_TAG2; 

    }else{ 
     UIImage* sourceImage1 = self.outletImageView.image; 
     flippedImage = [UIImage imageWithCGImage:sourceImage1.CGImage scale:1.0 orientation: UIImageOrientationUp]; 
     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]; 

} 

回答

-1

而不是[imgViewArray removeObject:self.outletImageView];

写:[imgViewArray removeObjectAtIndex:index1];

事后你需要检查flippedImage.imageOrientation == UIImageOrientationUp,看看它是否反转与否并做出相应的变化。

您可以验证画面上有通过访问信息词典[info objectForKey:@"Orientation"]

这里什么方向是将返回基于您以前的检查新的理论取向的方法(您想通过flippedImage.imageOrientation作为参数):

- (UIImageOrientation)orientation:(int)orientation 
{ 
    UIImageOrientation newOrientation; 
    switch (orientation) 
    { 
     case 1: 
      newOrientation = UIImageOrientationUp; 
      break; 
     case 2: 
      newOrientation = UIImageOrientationUpMirrored; 
      break; 

    } 
    return newOrientation; 
} 
+0

这并没有解决问题,它仍然没有检测到当前的方向,然后镜像它。这是以与以前完全相同的方式工作 –

+0

请参阅更新的答案。 – Winston