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])

                

            }

            

            

        }

    }

    改写为工具类中的方法

    期待中

  • 相关阅读:
    PAT A1094 The Largest Generation (25 分)——树的bfs遍历
    PAT A1055 The World's Richest (25 分)——排序
    PAT A1052 Linked List Sorting (25 分)——链表,排序
    PAT A1076 Forwards on Weibo (30 分)——图的bfs
    辅导员
    辅导员面试
    C程序设计
    Excel VBA 基本概念
    Excel函数
    导入excel表的数据到数据库ssh
  • 原文地址:https://www.cnblogs.com/sundaymac/p/10334961.html
Copyright © 2011-2022 走看看