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
// 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
//: 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
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
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
//: 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
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)) ") |
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 | |
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") |
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 | |
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 |
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
#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]) |