Skip to content

Instantly share code, notes, and snippets.

@tempire
tempire / take_screenshot.swift
Created August 5, 2015 07:34
Take raw screenshot on ios with swift
func takeScreenshot(view: UIView) -> UIImageView {
UIGraphicsBeginImageContext(view.frame.size)
view.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
return UIImageView(image: image)
}
@tempire
tempire / random_data_in.swift
Created June 26, 2015 06:20
Generate arbitrary size of bytes in swift
let max = 1024*1024*200
var data = NSMutableData(capacity: max)
for var i=0; i<max/4; ++i {
var random = arc4random()
data?.appendBytes(&random, length: 4)
}
@tempire
tempire / reflect.swift
Created June 24, 2015 00:42
Simple swift reflection
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
class Hello {
var id: String
init(id: String) {
OperationQueue(
SerialOperation("S1",
SampleOp("op1", timeout: 1),
SampleOp("op2", timeout: 1)
),
ConcurrentOperation("C1",
SampleOp("op3", timeout: 1),
SampleOp("op4", timeout: 1),
ConcurrentOperation("C2",
SampleOp("op5", timeout: 1),
oq(
TakeInventoryAndDiffAgainstServer(model: self),
GetInventoryFromServer(model: self),
SaveInventory(model: self),
bo { op in
return true
}
).start()
import Foundation
class SampleOp : Operation {
private var timeout: Double
init(timeout: Double) {
self.timeout = timeout
super.init()
}
extension NSManagedObject {
class func createEntity<T: NSManagedObject>(context: NSManagedObjectContext) -> T {
return NSEntityDescription.insertNewObjectForEntityForName(NSStringFromClass(T), inManagedObjectContext: context) as! T
}
}
class Customer: NSManagedObject {
@NSManaged var id: NSNumber
}
@tempire
tempire / timestables
Last active August 29, 2015 14:20 — forked from anonymous/timestables
#!/usr/bin/perl
#use strict;
use warnings;
use Switch;
use Data::Printer;
my ($choice, $times, $key, $value);
my @array = (1, 2, 3, 4, 5, 6, 7, 8, 9);
#my %hash_0 = map { $_ => $_ * 0 } @array;
@tempire
tempire / swift_draw_image_on_pdf.swift
Created April 25, 2015 09:24
swift draw image on pdf
func drawOnPDF(path: String) {
// Get existing Pdf reference
let pdf = CGPDFDocumentCreateWithURL(NSURL(fileURLWithPath: path))
// Get page count of pdf, so we can loop through pages and draw them accordingly
let pageCount = CGPDFDocumentGetNumberOfPages(pdf);
// Write to file
UIGraphicsBeginPDFContextToFile(path, CGRectZero, nil)
#!/usr/bin/env perl
use Mojolicious::Lite;
use POSIX 'strftime';
my $ua = Mojo::UserAgent->new;
get '/' => sub {
my $c = shift;
$c->delay(