Skip to content

Instantly share code, notes, and snippets.

View westerlund's full-sized avatar

Simon Westerlund westerlund

View GitHub Profile
//
// URLSessionOperation.swift
// time
//
// Created by Simon Westerlund on 2016-10-23.
// Copyright © 2016 Simon Westerlund. All rights reserved.
//
import UIKit
@westerlund
westerlund / Mirror.swift
Created August 17, 2016 13:17
Unwrap .Optional in an Any
extension Mirror {
private func unwrap(any: Any) -> Any? {
let mirror = Mirror(reflecting: any)
if mirror.displayStyle != .Optional {
return any
}
guard let first = mirror.children.first else {
return nil
}
@westerlund
westerlund / CGRect.swift
Created May 11, 2016 08:10
CGRect + resize keeping aspect
extension CGRect {
func resized(size: CGSize, aspectFill: Bool) -> CGRect {
var resizedRect = CGRect.zero;
let aspectWidth = size.width / self.size.width
let aspectHeight = size.height / self.size.height
let aspectRatio = aspectFill ? max(aspectWidth, aspectHeight) : min(aspectWidth, aspectHeight)
resizedRect.size.width = self.size.width * aspectRatio
resizedRect.size.height = self.size.height * aspectRatio
@westerlund
westerlund / UISlider.swift
Created May 3, 2016 09:19
Snapping UISlider in Swift
final class SnappingSlider: UISlider {
override var value: Float {
set { super.value = newValue }
get {
return round(super.value * 1.0) / 1.0
}
}
}
@westerlund
westerlund / ffmpeg_commands.md
Last active November 15, 2017 08:29
Common ffmpeg commands

common ffmpeg commands

common arguments

-y overwrite without asking
-r frame rate
-vf eq=1:0:1.1:1:1:1:1:1 change colors. contrast : brightness : saturation : gamma : gamma r : gamma g : gamma b : weight
-vf scale=width:height scale/resize. -1 will leave preserved ratio, for instance 320:-1 will resize to 320 in width and keep ratio
-ss seek to position
-t duration, trim to a certain length

@westerlund
westerlund / json.swift
Last active December 22, 2015 11:11
Command line tool to pretty-print piped JSON in Swift
//
// Created by Simon Westerlund on 22/12/15.
// Copyright © 2015 Simon Westerlund. All rights reserved.
//
import Foundation
func main() -> Int32 {
let data = NSFileHandle.fileHandleWithStandardInput().readDataToEndOfFile()

Indisk linsgryta

Ingredienser:

  • 1 burk skalade tomater (eller 5 tomater)
  • 1 gul lök
  • 1 msk riven ingefära
  • 2 tsk mald koriander
  • 1 tsk gurkmeja
  • 1 1/2 dl röda linser
@westerlund
westerlund / cocoa-drawing.swift
Last active November 29, 2015 10:33 — forked from randomsequence/cocoa-drawing.swift
Drawing images with CGContext and NSGraphicsContext in Swift
//: Playground - noun: a place where people can play
import Cocoa
let bounds = CGRectMake(0, 0, 100, 100);
func DrawImageInCGContext(size: CGSize, drawFunc: (context: CGContextRef, rect: CGRect) -> Void) -> NSImage? {
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)
if let context = CGBitmapContextCreate(
@westerlund
westerlund / NSImageDisk.swift
Created November 29, 2015 10:25
Write NSImage to disk
extension NSImage {
func writeToFile(file: String, atomically: Bool, usingType type: NSBitmapImageFileType) -> Bool {
let properties = [NSImageCompressionFactor: 1.0]
guard
let imageData = TIFFRepresentation,
imageRep = NSBitmapImageRep(data: imageData),
fileData = imageRep.representationUsingType(type, properties: properties) else {
return false
}
return fileData.writeToFile(file, atomically: atomically)
@westerlund
westerlund / dispatch.swift
Created June 23, 2015 14:08
Dispatch after swift
func dispatch(#after: NSTimeInterval, queue: dispatch_queue_t = dispatch_get_main_queue(), #closure: dispatch_block_t) {
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(Double(after) * Double(NSEC_PER_SEC)))
dispatch_after(time, dispatch_get_main_queue(), closure)
}
func dispatch(#after: NSTimeInterval, closure: dispatch_block_t) {
dispatch(after: after, closure: closure)
}
dispatch(after: 0.5, { () -> Void in