2016-03-22 7 views
-1

私は2つの画像を切り替えたいので、魚とfishaと呼ぶことができます。 UIImageViewはplayerと呼ばれるため、コードは次のようになります。スイフト[UIImage]がSwiftに設定されました

@IBOutlet weak var player: UIImageView! 
var images: [UIImage] = [] 
images.append(UIImage(named: "fish")!) 
images.append(UIImage(named: "fisha")!) 
player.frame = CGRectMake((screenWidth-px)/2, (screenHeight-py)/2, px, py) 
player.animationImages = images 
player.animationDuration = 0.7 
player.startAnimating() 

これはすべて問題なく動作します。しかし、今、私はplayer2というUIImageViewを作成したいと言っています。これは、プレーヤーのミラーバージョンでなければなりません。私はそれが正常に動作することを期待しplayer2を作るために、コードのこの変更に伴い

func flipH(im:UIImage)->UIImage { 
    var newOrient:UIImageOrientation 
    switch im.imageOrientation { 
    case .Up: 
     newOrient = .UpMirrored 
    case .UpMirrored: 
     newOrient = .Up 
    case .Down: 
     newOrient = .DownMirrored 
    case .DownMirrored: 
     newOrient = .Down 
    case .Left: 
     newOrient = .RightMirrored 
    case .LeftMirrored: 
     newOrient = .Right 
    case .Right: 
     newOrient = .LeftMirrored 
    case .RightMirrored: 
     newOrient = .Left 
    } 
    return UIImage(CGImage: im.CGImage!, scale: im.scale, orientation: newOrient) 
    } 

この機能を使用します。

@IBOutlet weak var player2: UIImageView! 
var images: [UIImage] = [] 
images.append(flipH(UIImage(named: "fish")!)) 
images.append(flipH(UIImage(named: "fisha")!)) 
player2.frame = CGRectMake((screenWidth-px)/2, (screenHeight-py)/2, px, py) 
player2.animationImages = images 
player2.animationDuration = 0.7 
player2.startAnimating() 

ただし、それはありません。 Player2はプレーヤーと同じです。誰にも何か考えがありますか?

おかげで、 トム

答えて

0

UIImageのorientationは、あなたはそれが意味どう思うかという意味ではありませんので、それはです。それはある種の魔法のイメージトランスではありません。画像を反転する場合は、を反転してにします(つまり、反転したCTMを使用して画像を描画します)。

例えば、ここでイメージビューに表示された画像があります:

enter image description here

ここで同じ画像だが、画像ビューに表示、右から左をひっくり返し:

enter image description here

下位のコードでは、最初のイメージをCTMフリップグラフィックスコンテキストに描画して2番目のイメージを取得します。

+0

ああ、私が望むようにプログラムでUIImagesを反転する方法はありませんか? –

+0

ありがとうございます。あなたがやっていることだけではありません。 – matt

関連する問題