churabou / iOS-develop-blog

0 stars 0 forks source link

5月5日(土) imagecontextのtransform #21

Open churabou opened 6 years ago

churabou commented 6 years ago

このextensionめっちゃコード量減った。endContextとか呼び忘れない!!


extension CGContext {

    //opaque:
    //bitmap が 不透明な場合、opaqueをtrueにすることでalpha値が無視されoptimizeされる。
    //scale:
    //0.0の時はthe scale factor is set to the scale factor of the device’s main screen.
    static func createImage(_ size: CGSize,
                            _ opaque: Bool = false,
                            _ scale: CGFloat = 0.0,
                            _ closure: ((CGContext)->Swift.Void)) -> UIImage {

        UIGraphicsBeginImageContextWithOptions(size, opaque, scale)

        guard let ctx = UIGraphicsGetCurrentContext() else {
            fatalError("ctx not found")
        }

        closure(ctx)

        ctx.data

        guard let img = UIGraphicsGetImageFromCurrentImageContext() else {
              fatalError("failed to get image from current ctx")
        }
        UIGraphicsEndImageContext()
        return img
    }
}

今まで感覚でtransfromやってたけど、やっぱサンプルあった方が迷わない。

x,y,にそれぞれscaleして同じrectを書いて見た結果。


            ctx.setFillColor(UIColor.white.cgColor)
            ctx.fill(CGRect.init(origin: .zero, size: size))

            //通常の状態
            ctx.setFillColor(UIColor.red.cgColor)
            ctx.fill(.init(100,100,100,100))

            //Yに1/2倍
            ctx.setFillColor(UIColor.yellow.cgColor)
            ctx.scaleBy(x: 1, y: 1/2)
            ctx.fill(.init(100,100,100,100))
            ctx.scaleBy(x: 1, y: 2)

            //Yに2倍
            ctx.scaleBy(x: 1, y: 2)
            ctx.setFillColor(UIColor.green.cgColor)
            ctx.fill(.init(100,100,100,100))
            ctx.scaleBy(x: 1, y: 1/2)

            //Xに1/2倍
            ctx.scaleBy(x: 1/2, y: 1)
            ctx.setFillColor(UIColor.orange.cgColor)
            ctx.fill(.init(100,100,100,100))
            ctx.scaleBy(x: 2, y: 1)

            //Xに2倍
            ctx.scaleBy(x: 2, y: 1)
            ctx.setFillColor(UIColor.blue.cgColor)
            ctx.fill(.init(100,100,100,100))
            ctx.scaleBy(x: 1/2, y: 1)

trans

一応全体のコード。


        let size = CGSize.equal(to: 500)

        let img =  CGContext.createImage(size, false, 0.0) { ctx in

            ctx.setFillColor(UIColor.white.cgColor)
            ctx.fill(CGRect.init(origin: .zero, size: size))

            //通常の状態
            ctx.setFillColor(UIColor.red.cgColor)
            ctx.fill(.init(100,100,100,100))

            //Yに1/2倍
            ctx.setFillColor(UIColor.yellow.cgColor)
            ctx.scaleBy(x: 1, y: 1/2)
            ctx.fill(.init(100,100,100,100))
            ctx.scaleBy(x: 1, y: 2)

            //Yに2倍
            ctx.scaleBy(x: 1, y: 2)
            ctx.setFillColor(UIColor.green.cgColor)
            ctx.fill(.init(100,100,100,100))
            ctx.scaleBy(x: 1, y: 1/2)

            //Xに1/2倍
            ctx.scaleBy(x: 1/2, y: 1)
            ctx.setFillColor(UIColor.orange.cgColor)
            ctx.fill(.init(100,100,100,100))
            ctx.scaleBy(x: 2, y: 1)

            //Xに2倍
            ctx.scaleBy(x: 2, y: 1)
            ctx.setFillColor(UIColor.blue.cgColor)
            ctx.fill(.init(100,100,100,100))
            ctx.scaleBy(x: 1/2, y: 1)
        }

        let v = UIView()
        v.bounds.size = .equal(to: 300)
        v.center = view.center
        v.layer.contents = img.cgImage
        view.addSubview(v)