Skip to content

Instantly share code, notes, and snippets.

View epatel's full-sized avatar

Edward Patel epatel

View GitHub Profile
@epatel
epatel / NSObject+SPInvocationGrabbing.h
Created September 9, 2010 21:11 — forked from nevyn/NSObject+SPInvocationGrabbing.h
NSObject+SPInvocationGrabbing
#import <Foundation/Foundation.h>
@interface SPInvocationGrabber : NSObject {
id _object;
NSInvocation *_invocation;
int frameCount;
char **frameStrings;
BOOL backgroundAfterForward;
BOOL onMainAfterForward;
BOOL waitUntilDone;
@epatel
epatel / gist:948970
Created April 29, 2011 20:25
flattenArray(array) -- flattens a hierarchy of NSArrays (should be a category I guess) [reason https://twitter.com/ikenndac/status/63609670182502400 -- but not a oneliner]
NSArray *flattenArray(NSArray *arr) {
NSMutableArray *collector = [NSMutableArray array];
for (id obj in arr) {
if ([obj isKindOfClass:[NSArray class]])
[collector addObjectsFromArray:flattenArray(obj)];
else
[collector addObject:obj];
}
return collector;
}
@epatel
epatel / bezier.cpp
Created July 26, 2011 19:33
Simple straight forward bezier in c++
#include <stdio.h>
struct point {
float x;
float y;
};
struct bezier {
float x0;
float y0;
@epatel
epatel / gist:1112862
Created July 29, 2011 00:08
100 words for jekyll feeds
# Added to filters.rb
def html_truncatewords(input, words = 15, truncate_string = "...")
doc = Hpricot.parse(input)
(doc/:"text()").to_s.split[0..words].join(' ') + truncate_string
end
# Used in atom.xml
<content type="html">{{ post.content | html_truncatewords:100 | xml_escape }}</content>
@epatel
epatel / cars.cpp
Created July 29, 2011 19:06
Counting words (car names) and sorting occurrences
#include <iostream>
#include <list>
#include <map>
using namespace std;
bool my_pair_compare(pair<string,int> &a, pair<string,int> &b) {
return a.second > b.second;
}
@epatel
epatel / UIImage+NSCoder.h
Created September 11, 2011 18:04
UIImage encoderWithCoder: and initWithCoder: (PNG style)
#import <Foundation/Foundation.h>
@interface UIImage (NSCoder)
- (void)encodeWithCoder:(NSCoder*)encoder;
- (id)initWithCoder:(NSCoder*)decoder;
@end
@epatel
epatel / md5.h
Created September 11, 2011 18:08
md5 for NSString and NSData
@interface NSString (md5)
- (NSString*)md5;
@end
@interface NSData (md5)
- (NSString*)md5;
@end
@epatel
epatel / NSObject+AssociatedObject.h
Created September 11, 2011 18:10
Adding AssociatedObject to NSObject
#import <Foundation/Foundation.h>
@interface NSObject (AssociatedObject)
- (id)associatedObjectForKey:(void*)key;
- (void)setAssociatedObject:(id)object forKey:(void*)key;
@end
@epatel
epatel / gist:1345686
Created November 7, 2011 18:05
Handy lines to set tint color of a UISlider (pre iOS5)
/* setting tint on slider */
UIGraphicsBeginImageContextWithOptions(CGSizeMake(20, 7), NO, 1.0);
[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(1, 0, 18, 7)
cornerRadius:3.0] addClip];
[[UIColor grayColor] setFill];
[[UIBezierPath bezierPathWithRect:CGRectMake(9, 0, 20, 7)] fill];
[tintColor setFill];
[[UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 5+5, 7)] fill];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(1, 1, 18, 7)
@epatel
epatel / get_pixel_values.m
Created November 18, 2011 22:15
Get the RGBA values for a pixel from an image
CGImageRef image = uiimage.CGImage;
NSUInteger width = CGImageGetWidth(image);
NSUInteger height = CGImageGetHeight(image);
// Setup 1x1 pixel context to draw into
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char rawData[4];
int bytesPerPixel = 4;
int bytesPerRow = bytesPerPixel;
NSUInteger bitsPerComponent = 8;