2014-04-17 26 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