Skip to content

Instantly share code, notes, and snippets.

View KrisYu's full-sized avatar

Xue Yu KrisYu

View GitHub Profile
@KrisYu
KrisYu / UIImage_to_pdf.swift
Created July 9, 2017 22:18
save UIImage as pdf file
//: 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)!
@KrisYu
KrisYu / NSImage_to_pdf.swift
Last active February 23, 2024 10:21
convert NSImage to pdf, basically the same as UIImage
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
@KrisYu
KrisYu / simple_log_file.swift
Created July 16, 2017 00:40
A simple class to read and write to a specific plist.
import Cocoa
class LogFile {
static var logPath: String? {
get {
let paths = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory,
.userDomainMask,
true)
@KrisYu
KrisYu / captureScreen.swift
Created August 13, 2017 03:00
capture screen using terminal commands in swift
//: 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"
@KrisYu
KrisYu / saveImage.swift
Last active July 3, 2022 11:55
Save image to file on macOS using swift, answers found from SO
// 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
@KrisYu
KrisYu / UIImage+PixelColor.swift
Created September 29, 2017 01:49 — forked from marchinram/UIImage+PixelColor.swift
iOS Swift UIImage subscript extension to get pixel color
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
}
class Person: NSObject, NSCoding {
var firstName: String
var lastName: String
override var description: String {
return self.firstName + " " + self.lastName
}
struct Person: Codable {
var firstName: String
var lastName: String
var name: Data? {
return try? PropertyListEncoder().encode(self)
}
class Person: NSObject, Codable {
var firstName: String
var lastName: String
override var description: String {
return self.firstName + " " + self.lastName
}
//
// HorizontalScrollerView.swift
// RWBlueLibrary
//
// Created by Xue Yu on 2/4/18.
// Copyright © 2018 Razeware LLC. All rights reserved.
//
import UIKit
// 把 DataSource 和 Delegate 协议分开,这样更优雅,并且可以减少不必要的 @objc 的使用