Skip to content

Instantly share code, notes, and snippets.

View kgn's full-sized avatar

David Keegan kgn

View GitHub Profile
@kgn
kgn / gist:3012995
Created June 28, 2012 18:11
Noise in Cocoa
+ (void)drawNoiseWithOpacity:(CGFloat)opacity inRect:(CGRect)rect{
static UIImage *noiseImage = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
NSUInteger width = 64, height = 64;
NSUInteger size = width*height;
char *rgba = (char *)malloc(size); srand(124);
for(NSUInteger i=0; i < size; ++i){rgba[i] = rand()%256;}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapContext =
@kgn
kgn / FadeScrollView.h
Created July 26, 2012 06:39 — forked from MaximKeegan/FadeScrollView.h
Fade edges UIScrollView
#import <UIKit/UIKit.h>
@interface SNFadeScrollView : UIScrollView
@property (nonatomic) BOOL fadeTop;
@property (nonatomic) BOOL fadeBottom;
@end
@kgn
kgn / Localization.py
Created December 20, 2012 19:34
Copy the strings files downloaded from icanlocalize.com back into a Xcode project.
import os, sys
import shutil, fnmatch
root = os.path.dirname(os.path.realpath(__file__))
locations = {
'Localizable': os.path.join(root, 'Localization'),
'Root': os.path.join(root, 'Settings.bundle'),
}
@kgn
kgn / cleanup_images.py
Created June 7, 2013 16:07
Delete images that are not used in an app
import os
import re
import sys
projectRoot = sys.argv[1]
imageNamedRegex = re.compile('Named:@"([^"]+)"')
resourceRegEx = re.compile('<string key="NSResourceName">([^<]+)</string>')
def rootImageName(imageName):
imageName = re.sub('.png$', '', imageName)
@kgn
kgn / gist:5986379
Last active April 13, 2018 13:01
A UIImage category class method for drawing icon font ligatures to images
+ (UIImage *)iconWithFont:(UIFont *)font named:(NSString *)iconNamed forSize:(CGFloat)fontSize{
static NSCache *cache = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
cache = [[NSCache alloc] init];
});
NSString *identifier = [NSString stringWithFormat:@"%@%@%f", font.fontName, iconNamed, fontSize];
UIImage *image = [cache objectForKey:identifier];
if(image == nil){
@kgn
kgn / gist:6153852
Last active December 20, 2015 15:28
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname( __file__ ), 'ParsePy'))
from parse_rest.user import User as PFUser
from parse_rest.datatypes import Object as PFObject
from parse_rest.installation import Installation as PFInstallation
from parse_rest.connection import register as PFRegister
from parse_rest.connection import ParseBatcher as PFBatcher
PFRegister(PARSE_APPLICATION_ID, PARSE_REST_API_KEY, master_key=PARSE_MASTER_KEY)
@kgn
kgn / cleanup_images.py
Created September 8, 2013 22:10
Python script to cleanup unused images from an iOS app
import os
import re
import sys
projectRoot = sys.argv[1]
imageNamedRegex = re.compile('Named:@"([^"]+)"')
resourceRegEx = re.compile('<string key="NSResourceName">([^<]+)</string>')
def rootImageName(imageName):
imageName = re.sub('.png$', '', imageName)
@kgn
kgn / gist:7202655
Created October 28, 2013 19:04
Handy Auto Layout
CGFloat distance = 10;
// Center two views side-by-side
[parentItem addConstraint:[NSLayoutConstraint constraintWithItem:leftItem attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationLessThanOrEqual toItem:parentItem attribute:NSLayoutAttributeCenterX multiplier:1 constant:-distance]];
[parentItem addConstraint:[NSLayoutConstraint constraintWithItem:rightItem attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationLessThanOrEqual toItem:parentItem attribute:NSLayoutAttributeCenterX multiplier:1 constant:distance]];
// Center two views, one ontop of the other
[parentItem addConstraint:[NSLayoutConstraint constraintWithItem:topItem attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationLessThanOrEqual toItem:parentItem attribute:NSLayoutAttributeCenterY multiplier:1 constant:-distance]];
[parentItem addConstraint:[NSLayoutConstraint constraintWithItem:bottomItem attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationLessThanOrEqual toItem:parentItem attribute:NSLayoutAttributeCenterY multiplier
@kgn
kgn / AppReviews
Created June 9, 2015 23:02
App Reviews - Python script to retrieve App Store reviews and save them to a CSV file
#!/usr/bin/env python
try:
# For Python 3.0 and later
from urllib.request import urlopen
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen
import json
@kgn
kgn / JSONParse.swift
Created March 15, 2016 05:19
Crazy JSON parsing in Swift
let twitterUser: String?
if let data = serverResponse {
if let json = try? NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String: AnyObject] {
if let user = json?["user"] as? [String: AnyObject] {
if let accounts = user["accounts"] as? [AnyObject] {
if let twitter = accounts.first as? [String: AnyObject] {
twitterUser = twitter["user"] as? String
}
}
}