This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
let a = #imageLiteral(resourceName: "hot.png") | |
func createPDF(image: UIImage) -> NSData? { | |
let pdfData = NSMutableData() | |
let pdfConsumer = CGDataConsumer(data: pdfData as CFMutableData)! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
let a = #imageLiteral(resourceName: "hot.png") | |
extension NSImage { | |
var toCGImage: CGImage { | |
var imageRect = NSRect(x: 0, y: 0, width: size.width, height: size.height) | |
guard let image = cgImage(forProposedRect: &imageRect, context: nil, hints: nil) else { | |
abort() | |
} | |
return image |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
class LogFile { | |
static var logPath: String? { | |
get { | |
let paths = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, | |
.userDomainMask, | |
true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import Cocoa | |
public func caputreRegion(toURL fileUrl: URL) -> URL { | |
let fileUrlPath = fileUrl.path | |
let task = Process() | |
task.launchPath = "/usr/sbin/screencapture" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// plain write to image | |
@discardableResult func writeCGImage(_ image: CGImage, to destinationURL: URL) -> Bool { | |
guard let destination = CGImageDestinationCreateWithURL(destinationURL as CFURL, kUTTypePNG, 1, nil) else { return false } | |
CGImageDestinationAddImage(destination, image, nil) | |
return CGImageDestinationFinalize(destination) | |
} | |
// There's a panel, save image png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIImage { | |
subscript (x: Int, y: Int) -> UIColor? { | |
if x < 0 || x > Int(size.width) || y < 0 || y > Int(size.height) { | |
return nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person: NSObject, NSCoding { | |
var firstName: String | |
var lastName: String | |
override var description: String { | |
return self.firstName + " " + self.lastName | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Person: Codable { | |
var firstName: String | |
var lastName: String | |
var name: Data? { | |
return try? PropertyListEncoder().encode(self) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person: NSObject, Codable { | |
var firstName: String | |
var lastName: String | |
override var description: String { | |
return self.firstName + " " + self.lastName | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// HorizontalScrollerView.swift | |
// RWBlueLibrary | |
// | |
// Created by Xue Yu on 2/4/18. | |
// Copyright © 2018 Razeware LLC. All rights reserved. | |
// | |
import UIKit | |
// 把 DataSource 和 Delegate 协议分开,这样更优雅,并且可以减少不必要的 @objc 的使用 |