0

I want to convert a NSView to a NSImage and display it. The NSView has subviews which are having 3dtransforms(CATransform3DRotate) applied to its layers.

layer.transform =  CATransform3DRotate(CATransform3DIdentity, 0.5, 1, 0, 0)

When the NSImage is produced the transforms are completely ignored from the NSView and returns a plain image without any transform effect.enter image description here

I am using this function to convert NSView to NSImage

 extension NSView {
     func image() -> NSImage {
        let imageRepresentation = bitmapImageRepForCachingDisplay(in: bounds)!        
        cacheDisplay(in: bounds, to: imageRepresentation)
        return NSImage(cgImage: imageRepresentation.cgImage!, size: bounds.size)
     }
}

From this Question I found out that transforms getting ignored while rendering is a common issue in iOS(UIKit) also, and the workaround that works for this problem is to use drawViewHierarchyInRect.

The complete code using drawViewHierarchyInRect in iOS -

UIGraphicsBeginImageContextWithOptions(view.bounds.size, true, 0)
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

unfortunately drawHierarchy is not available in OSx(Appkit) and there are no other alternative approaches available in my belief. I'm hoping someone has knowledge about this and will help me or point me towards the right direction. Thanks in advance.

vigu
  • 21
  • 4

0 Answers0