Skip to content

Instantly share code, notes, and snippets.

View justinHowlett's full-sized avatar

Justin Howlett justinHowlett

View GitHub Profile
@justinHowlett
justinHowlett / polydecoder.py
Created November 19, 2014 00:08
Google Polyline decoder
class GPolyDecoder(object):
def decode(self, encodedString):
index = 0
length = len(encodedString)
lat = 0
lng = 0
poly_lines = []
while index < length:
b = 0
@justinHowlett
justinHowlett / Swift Mirror
Created October 22, 2014 03:29
Basic mirroring in Swift
let mirror = reflect(objectInstance)
for index in 0 ..< mirror.count {
let (childKey, childMirror) = mirror[index]
println("key is \(childKey)")
println("value is \(childMirror.value)")
println("valuetype is \(childMirror.valueType)")
}
@justinHowlett
justinHowlett / lazyasyncgetter
Created July 8, 2014 04:37
Example of a lazy loaded synchronous background threaded singleton getter in Swift
let kGlobalDateFormatterSharedInstance = GlobalDateFormatterModel()
class GlobalDateFormatterModel: NSObject {
@lazy var defaultDomainDateFormatter: NSDateFormatter = self.initializeDefaultDomainDateFormatter()
class var sharedInstance:GlobalDateFormatterModel {
return kGlobalDateFormatterSharedInstance
}
@justinHowlett
justinHowlett / gist:5834778
Created June 21, 2013 22:21
Quick sort in Objective-C
-(void)quickSortArray:(NSMutableArray*)array fromLeftIndex:(NSUInteger)leftIndex toRightIndex:(NSUInteger)rightIndex{
NSUInteger chosenPivot = 0;
if (leftIndex == 0 && rightIndex == 0)
rightIndex = array.count - 1;
chosenPivot = leftIndex + ceil((rightIndex - leftIndex) * 0.5);
@justinHowlett
justinHowlett / gist:5834773
Last active November 20, 2016 19:34
Bubble sort in Objective-C
-(void)bubbleSortArray:(NSMutableArray*)unsortedArray{
while (TRUE) {
BOOL hasSwapped = NO;
for (int i=0; i<unsortedArray.count; i++){
/** out of bounds check */
if (i < unsortedArray.count-1){
@justinHowlett
justinHowlett / gist:5827739
Created June 20, 2013 23:40
UIView Category for producing a raster copy
#include <QuartzCore/QuartzCore.h>
@implementation UIView (Raster)
-(UIImage*)getRasterCopy{
/* returns UIImage of any UIView */
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0);
@justinHowlett
justinHowlett / gist:5827443
Last active December 18, 2015 18:40
Resign the keyboard no matter where it is in the application. (I usually wrap my utility methods in a C function)
void globalResignKeyboard(void){
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
for (UIView * view in [window subviews]){
resignKeyboardInSubviewsOfView(view);
}
}
void resignKeyboardInSubviewsOfView(UIView* view){
@justinHowlett
justinHowlett / gist:4611988
Last active July 9, 2024 11:53
Determine if a UIImage is generally dark or generally light
BOOL isDarkImage(UIImage* inputImage){
BOOL isDark = FALSE;
CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(inputImage.CGImage));
const UInt8 *pixels = CFDataGetBytePtr(imageData);
int darkPixels = 0;
int length = CFDataGetLength(imageData);
@justinHowlett
justinHowlett / gist:4611132
Created January 23, 2013 18:07
Find bottom non-transparent pixels in a UIImage
float bottomNonAlphaPixel(UIImage* inputImage){
float bottomNonAlphaPixel = 0;
float const neighboringPixelThreshold = 4;
CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(inputImage.CGImage));
const UInt8 *pixels = CFDataGetBytePtr(imageData);
UInt8 alphaThreshold = 20;
int bytesPerPixel = 4;