iOS:如何根据当前方向翻转图像?

时间:2014-04-17 13:48:41

标签: ios uiimage orientation

我正在尝试检查图像的当前方向并将其翻转到与当前相反的方向。

如果我的屏幕上只有1个元素,则此代码有效,但只要我增加数组就有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 个答案:

答案 0 :(得分:-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;  
}