Get Image from CALayer or NSView (swift 3)

后端 未结 1 500
情深已故
情深已故 2020-12-11 03:01

I was looking for a way to render a CALayer or NSView and get NSImage back.

I have a custom class, which is a subclass of NSView. At the beginning I simply make a gra

相关标签:
1条回答
  • 2020-12-11 03:23

    Here are some NSView options:

    extension NSView {
    
        /// Get `NSImage` representation of the view.
        ///
        /// - Returns: `NSImage` of view
    
        func image() -> NSImage {
            let imageRepresentation = bitmapImageRepForCachingDisplay(in: bounds)!
            cacheDisplay(in: bounds, to: imageRepresentation)
            return NSImage(cgImage: imageRepresentation.cgImage!, size: bounds.size)
        }
    
    }
    

    Or

    extension NSView {
    
        /// Get `Data` representation of the view.
        ///
        /// - Parameters:
        ///   - fileType: The format of file. Defaults to PNG.
        ///   - properties: A dictionary that contains key-value pairs specifying image properties.
        /// - Returns: `Data` for image.
    
        func data(using fileType: NSBitmapImageFileType = .PNG, properties: [String : Any] = [:]) -> Data {
            let imageRepresentation = bitmapImageRepForCachingDisplay(in: bounds)!
            cacheDisplay(in: bounds, to: imageRepresentation)
            return imageRepresentation.representation(using: fileType, properties: properties)!
        }
    
    }
    

    Some CALayer options:

    extension CALayer {
    
        /// Get `NSImage` representation of the layer.
        ///
        /// - Returns: `NSImage` of the layer.
    
        func image() -> NSImage {
            let width = Int(bounds.width * self.contentsScale)
            let height = Int(bounds.height * self.contentsScale)
            let imageRepresentation = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: width, pixelsHigh: height, bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: NSDeviceRGBColorSpace, bytesPerRow: 0, bitsPerPixel: 0)!
            imageRepresentation.size = bounds.size
    
            let context = NSGraphicsContext(bitmapImageRep: imageRepresentation)!
    
            render(in: context.cgContext)
    
            return NSImage(cgImage: imageRepresentation.cgImage!, size: bounds.size)
        }
    
    }
    

    Or

    extension CALayer {
    
        /// Get `Data` representation of the layer.
        ///
        /// - Parameters:
        ///   - fileType: The format of file. Defaults to PNG.
        ///   - properties: A dictionary that contains key-value pairs specifying image properties.
        ///
        /// - Returns: `Data` for image.
    
        func data(using fileType: NSBitmapImageFileType = .PNG, properties: [String : Any] = [:]) -> Data {
            let width = Int(bounds.width * self.contentsScale)
            let height = Int(bounds.height * self.contentsScale)
            let imageRepresentation = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: width, pixelsHigh: height, bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: NSDeviceRGBColorSpace, bytesPerRow: 0, bitsPerPixel: 0)!
            imageRepresentation.size = bounds.size
    
            let context = NSGraphicsContext(bitmapImageRep: imageRepresentation)!
    
            render(in: context.cgContext)
    
            return imageRepresentation.representation(using: fileType, properties: properties)!
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题