Search code examples
iosswiftuiimagecalayercgimage

CGImage in CALayer appears upside down


When adding an UIImage to a CALayer it appears upside down. I know it's an issue related to the conversion to a CGImage but i couldn't find any working solution. all attempt to fix the orientation has failed

   `guard let img = UIImage(named: selected)  else {return}
    guard let cg = img.cgImage  else {return}
    CATransaction.begin()
        let faceLayer = arr[indexPath.row]
        let myLayer = CALayer()
        myLayer.frame = faceLayer.bounds
        myLayer.contents = cg
        myLayer.opacity = 1.0
        faceLayer.opacity = 1.0
        myLayer.backgroundColor = UIColor.clear.cgColor
        faceLayer.addSublayer(myLayer)
        pathLayer?.addSublayer(faceLayer)
    CATransaction.commit()`

Solution

  • Eventually i just rotate the UIImage before converting it to CGImgae. This cane also be achieved by rotating the layer itself using affineTransform().