2009-07-17 4 views

答えて

22

次のようなものを使用してコンテキストを変換することができるはずです。

CGContextSaveGState(bitmapContext); 
CGContextTranslateCTM(bitmapContext, 0.0f, originalImage.size.height); 
CGContextScaleCTM(bitmapContext, 1.0f, -1.0f); 

// Draw here 
CGContextDrawImage(bitmapContext, CGRectMake(0, 0, originalImage.size.width, originalImage.size.height), oImageRef); 

CGContextRestoreGState(bitmapContext); 

翻訳は画像描画のために必要ではないかもしれないが、私は逆テキストを描画したいとき、私はそれを必要としていました。これがコンテキスト内で唯一のことであれば、コンテキストの状態を保存して復元する呼び出しを取り除くこともできます。

+1

。 UIGraphicsBeginImageContextの直後にそれを呼び出しました。クール! – Thanks

8

ブラッドのsolutionスウィフト3での拡張機能として:私はCGContextTranslateCTMとCGContextScaleCTMを必要

extension CGContext { 
    func drawFlipped(image: CGImage, rect: CGRect) { 
     saveGState() 
     translateBy(x: 0, y: rect.height) 
     scaleBy(x: 1, y: -1) 
     draw(image, in: rect) 
     restoreGState() 
    } 
}