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

                

            }

            

            

        }

    }

    改写为工具类中的方法

    期待中

  • 相关阅读:
    sql语句执行顺序
    ThinkPHP的入门学习目录结构及基础知识
    IE6的PNG透明解决方案
    用CSS画三角形
    position:sticky介绍 页面滚动导航条始终在最顶部的实现方法
    那些年我们一起清除过的浮动
    "自适应网页设计"到底是怎么做到的?其实并不难。
    jQuery formValidator表单验证插件(详解)
    学习10分钟,改变你的程序员生涯【转载】
    最差的时光 枯木
  • 原文地址:https://www.cnblogs.com/sundaymac/p/10334961.html
Copyright © 2011-2022 走看看