zoukankan      html  css  js  c++  java
  • swift之Mac中NSView视图里的截图【ScrollView中的内容截图】

    import Foundation

    import Cocoa

     在视图中的方法

    extension NSView {

        /****1.

         在drawRect方法中绘制

        使用Quartz2D绘图函数在视图上绘制圆角矩形

     ****/

        override func draw(_ dirtyRect: NSRect) {

            super.draw(dirtyRect)

            NSColor.blue.setFill()

            let frame = self.bounds

            let path = NSBezierPath()

            path.appendRoundedRect(frame, xRadius: 10, yRadius: 10)

            path.fill()

        }

        

        /***2

         在drawRect方法之外实现绘制,需要使用lockFocus方法锁定视图,完成绘图后在执行unlockFocus解锁

         drawRect方法与lockFocus锁定方式不能同时使用

         ***/

        func drawViewShape(){

            self.lockFocus()

            let text: NSString = "RoundedRect"

            let font = NSFont(name: "Palatino-Roman", size: 12)

            let attrs = [NSAttributedString.Key.font: font,NSAttributedString.Key.foregroundColor:NSColor.red]

            let location = NSPoint(x: 50, y: 50)

            text.draw(at: location, withAttributes: attrs)

            self.unlockFocus()

        }

        /****3

         视图截屏

         ***/

        func saveSelfAsImage() {

            self.lockFocus()

            let image = NSImage(data: self.dataWithPDF(inside: self.bounds))

            self.unlockFocus()

            let imageData = image!.tiffRepresentation

            let fileManager = FileManager.default

            //制定文件路径

            let path = "/Users/.../Documents/myCapture.png"

            fileManager.createFile(atPath: path, contents: imageData, attributes: nil)

            //保存结束后Finder中自动定位到文件路径

            let fileUrl = URL(fileURLWithPath: path)

            NSWorkspace.shared.activateFileViewerSelecting([fileUrl])

            

      }

            //如果视图比较大,是带滚动条的大视图,则按下面的方法处理可以保证获得整个滚动页面的截图

        func saveScrollViewAsImage() {

            //

            let pdfdata = self.dataWithPDF(inside: self.bounds)

    //        self.dataWithEPS(inside: self.bounds)  ????

            let imageRep = NSPDFImageRep(data: pdfdata)!

            let count = imageRep.pageCount

            for i in 0..<count {

                imageRep.currentPage = i

                let tempImage = NSImage()

                tempImage.addRepresentation(imageRep)

                let rep = NSBitmapImageRep(data: tempImage.tiffRepresentation!)

                let imageData = rep?.representation(using: NSBitmapImageRep.FileType.png, properties: [:])

                //NSBitmapImageRep.PropertyKey : Any

                let fileManager = FileManager.default

                //制定文件路径

                let path = "/Users/.../Documents/myCapture.png"

                fileManager.createFile(atPath: path, contents: imageData, attributes: nil)

                //保存结束后Finder中自动定位到文件路径

                let fileUrl = URL(fileURLWithPath: path)

                NSWorkspace.shared.activateFileViewerSelecting([fileUrl])

                

            }

            

            

        }

    }

    改写为工具类中的方法

    期待中

  • 相关阅读:
    未能加载文件或程序集''file:///D:/Program Files (x86)/ArcGIS/DeveloperKit10.0/DotNet/ESRI.ArcGIS.ADF.Local.dll'' 或它的某一个依赖项。试图加载格式不正确的程序。
    [GL]三维场景的组织
    一张图,把我震惊了【转】
    [WorldWind学习]8.Cache对象
    [WorldWind学习]6.World类
    七桥问题及一笔画
    VS下Qt4.8.4安装
    [WorldWind学习]5.相机对象
    [WorldWind学习]1.接触WorldWind项目
    [WorldWind学习]2.WorldWindow控件
  • 原文地址:https://www.cnblogs.com/sundaymac/p/10334961.html
Copyright © 2011-2022 走看看