Skip to content

Instantly share code, notes, and snippets.

View KrisYu's full-sized avatar

Xue Yu KrisYu

View GitHub Profile
@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
}
@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 / 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 / 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 / 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 / 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 / URLSession_Download_File.swift
Created July 9, 2017 05:03
URLSession download files
func downloadFile(url: URL) {
let downloadRequest = URLRequest(url: url)
URLSession.shared.downloadTask(with: downloadRequest) { location, response, error in
// To do check resoponse before saving
guard let tempLocation = location, error == nil else { return }
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last
do {
let fullURL = try documentDirectory?.appendingPathComponent((response?.suggestedFilename!)!)
try FileManager.default.moveItem(at: tempLocation, to: fullURL!)
print("saved at \(String(describing: fullURL)) ")
import Cocoa
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
struct iTunesRequestManager {
static func getSearchResult(_ query: String, results: Int, langString: String, completionHandler: @escaping ([[String: AnyObject]], NSError?) -> Void){
var urlComponents = URLComponents.init(string: "https://itunes.apple.com/search")
import Cocoa
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
if let url = URL(string: "https://api.github.com/users/octocat") {
URLSession.shared.dataTask(with: url){ data, response, error in
@KrisYu
KrisYu / kindnotes.py
Created June 22, 2017 18:44
use on Mac,split My Clippings.txt by booktitle ( 4 year old script
#filename: kindlesplit.py
f=open('My Clippings.txt').read()
notes = f.split('==========\r\n')
lines = []
booktitle = []
lines = notes[:]
for i in range(len(lines)):
lines[i]=lines[i].split('\r\n')
booktitle.append(lines[i][0])