Skip to content

Instantly share code, notes, and snippets.

@aspitz
aspitz / UIImage+Alpha
Created May 20, 2013 20:02
UIImage category that adds a method to create a white image based on the source images alpha channel
@implementation UIImage (Alpha)
- (UIImage *)alphaImage{
CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height);
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB(); // create an RGB Color Space
// create the context to do our drawing in
CGContextRef context = CGBitmapContextCreate(NULL, imageRect.size.width, imageRect.size.height, 8, imageRect.size.width * 4, rgbColorSpace, kCGImageAlphaPremultipliedLast);
// use the images alpha channel as a mask
CGContextClipToMask(context, imageRect, self.CGImage);
@aspitz
aspitz / NSWindow+Utilities
Created June 18, 2013 19:44
Category code to position a window based on the upper right hand corner of the screen
#import "NSWindow+Utilities.h"
@implementation NSWindow (Utilities)
- (void)setTopLeftRelativeToMainScreen:(CGPoint)topLeftPt{
CGRect mainScreenVisibleFrame = NSScreen.mainScreen.visibleFrame;
CGPoint pt = CGPointZero;
pt.x = topLeftPt.x;
pt.y = mainScreenVisibleFrame.origin.y + mainScreenVisibleFrame.size.height - topLeftPt.y;
[self setFrameTopLeftPoint:pt];
@aspitz
aspitz / NSUserDefaults+Utilities
Created June 18, 2013 19:46
Category code to store and retrieve CGPoint and CGRect
#import "NSUserDefaults+Utilities.h"
@implementation NSUserDefaults (Utilities)
- (CGPoint)CGPointForKey:(NSString *)key
{
CGPoint pt = CGPointZero;
NSArray *ptArray = [self objectForKey:key];
if (ptArray != nil)
@aspitz
aspitz / Remove SVN
Created June 20, 2013 13:34
This bash command removes SVN from a folder and all it's children
find . -name ".svn" -exec rm -Rf {} \;
@aspitz
aspitz / .gitignore
Created June 20, 2013 13:40
Base Git Ignore file for Xcode project
.DS_Store
*.swp
*~.nib
build/
*.pbxuser
*.perspective
*.perspectivev3
@aspitz
aspitz / ZDrawView
Last active December 19, 2015 12:18
A subclass of UIView that allows a developer to draw in a view without having to subclass UIView
typedef void (^DrawBlock)(CGContextRef currentContext, CGRect drawRect, UIView *selfView);
@interface ZDrawView : UIView
@property (nonatomic, copy) DrawBlock drawBlock;
- (instancetype)initWithBlock:(DrawBlock)block;
+ (instancetype)viewWithBlock:(DrawBlock)block;
@aspitz
aspitz / OrderedDictionary
Created July 31, 2013 13:49
A dictionary that keeps track of the order that elements were added to it
@interface OrderedDictionary : NSObject <NSCopying, NSCoding>{
NSMutableDictionary *_dictionary;
NSMutableArray *_array;
}
@property (readonly) NSUInteger count;
- (id)init;
- (id)initWithCapacity:(NSUInteger)capacity;
+ (id)dictionary;
@aspitz
aspitz / ZZiptastic
Last active December 20, 2015 18:29
A simple class that makes the Ziptastic REST service available to iOS and OSX developers
#import <Foundation/Foundation.h>
extern NSString *const ZZiptasticCityKey;
extern NSString *const ZZiptasticStateKey;
extern NSString *const ZZiptasticCountryKey;
extern NSString *const ZURLHTTPErrorCode;
typedef void (^ZZiptasticBlock)(NSDictionary *location, NSError *error);
@aspitz
aspitz / base64
Created August 13, 2013 12:42
Encode/decode a binary file into/out of base64 from the command line
To encode
> base64 -i [source.binary] -o [destination.base64]
To decode
> base64 -D -i [source.base64] -o [destination.binary]
To encode directly into the clipboard
> base64 < [source.binary] | pbcopy
@aspitz
aspitz / UIImage+UIViewCapture
Created August 21, 2013 13:19
Category code to capture a UIView as a UIImage
#import "UIImage+UIViewCapture.h"
#import <QuartzCore/QuartzCore.h>
@implementation UIImage (UIViewCapture)
+ (UIImage *)imageWithView:(UIView *)view{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();