Skip to content

Instantly share code, notes, and snippets.

View swissmanu's full-sized avatar
👻

Manuel Alabor swissmanu

👻
View GitHub Profile
@swissmanu
swissmanu / gist:2642579
Created May 9, 2012 07:06
simplyfied requestAnimationFrame
/**
* An even more common approach to look for vendor specific implementations in
* the browsers javascript object model.
* lookUpImplementation(vendors,subject,functionName,replacement) is reusable
* for other tasks too :-)
*/
var vendors = ['webkit','moz','o','ms'];
window.requestAnimationFrame = lookUpImplementation(
vendors, window, 'requestAnimationFrame'
,function(step) {
@swissmanu
swissmanu / gist:4284339
Created December 14, 2012 10:27
Draws a smiley face under iOS. Pass a mood value ranging from 0 to 1 for making it happy (or sad :( )
/**
* Draws a smiley inside the given rectangle. The mood value (0...1) is used
* to make the smiley happy or sad.
*
* @param rect Rectangle to draw the smiley into
* @param mood Happy (= 1) or sad (= 0) or anything in between
*/
-(void)drawSmileyInRect:(CGRect)rect withMood:(float)mood {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
@swissmanu
swissmanu / Helpers.h
Last active December 11, 2015 04:29
Assuming you have a crappy model which contains properties with a locale identifier as suffix (like "title_de", "title_en" etc), `localizedProperty(NSString*, id)` returns a specific translation of that property. This Gist provides a global function and is ready to use :-)
NSString* localizedProperty(NSString* propertyName, id fromObject);
@swissmanu
swissmanu / gist:4663178
Last active December 11, 2015 21:29
A global helper function to draw a vertical gradient using CoreGraphics. Pass a NSArray with UIColors and a plain C-array with step-location for each color. Don't invoke too often and when, don't use to many color steps. Each color needs to be converted using an expensive for-iteration.
// NSArray *colors = @[[UIColor redColor], [UIColor greenColor], [UIColor blueColor]];
// CGFloat locations[] = {0,.5,1};
// DrawVerticalGradient(rect, colors, locations, ctx);
void DrawVerticalGradient(CGRect rect, NSArray* colors, const CGFloat steps[], CGContextRef ctx) {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSMutableArray *cgColors = [NSMutableArray arrayWithCapacity:colors.count];
for (UIColor *color in colors) {
[cgColors addObject:(__bridge id)(color.CGColor)];
}
@swissmanu
swissmanu / gist:4682477
Created January 31, 2013 12:18
Following functions make the the creation and drawing of programmatical generated UIImages a little bit easier. Call `CreateBitmapContext(CGSize)` to get a CGContextRef you can draw into. Afterwards, just call `CreateUIImageFromBitmap(CGContextRef)` to return a common usable UIImage instance and free up any used resources. This Gist is based on h…
CGContextRef CreateBitmapContext(CGSize size) {
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(nil, size.width, size.height, 8, size.width * (CGColorSpaceGetNumberOfComponents(space) + 1), space, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(space);
return ctx;
}
UIImage* CreateUIImageFromBitmapContext(CGContextRef ctx) {
CGImageRef cgImage = CGBitmapContextCreateImage(ctx);
@swissmanu
swissmanu / NSArray+ExtractValuesAsArray.h
Last active December 12, 2015 07:38
Create a NSArray with a specific value of the items of another NSArray.
@interface NSArray (ExtractValuesAsArray)
/** Creates a new NSArray with the specific value of the items of another array. Ensure that the items of the array are KVC compliant since a key path is used to gather the values.
*/
+(NSArray*)arrayWithValuesOfArrayItems:(NSArray*)array keyPath:(NSString*)keyPath;
@end
@swissmanu
swissmanu / gist:4943356
Created February 13, 2013 09:38
check if a CLLocationCoordinate2D is inside MKCoordinateRegion. this is a simplified version of http://stackoverflow.com/a/10574327/368959
-(BOOL)isCoordinate:(CLLocationCoordinate2D)coordinate insideRegion:(MKCoordinateRegion)region {
CLLocationCoordinate2D center = region.center;
CLLocationCoordinate2D northWestCorner, southEastCorner;
northWestCorner.latitude = center.latitude - (region.span.latitudeDelta / 2.0);
northWestCorner.longitude = center.longitude - (region.span.longitudeDelta / 2.0);
southEastCorner.latitude = center.latitude + (region.span.latitudeDelta / 2.0);
southEastCorner.longitude = center.longitude + (region.span.longitudeDelta / 2.0);
return(coordinate.latitude >= northWestCorner.latitude &&
@swissmanu
swissmanu / gist:5382296
Created April 14, 2013 11:00
simple startup script for sabnzbd, couchpotato and sickbeard on a synology nas.
#!/bin/sh
SABNZBD_CONFIG=/root/.sabnzbd/sabnzbd.ini
SABNZBD_HOST=--HOST_ADDRESS--
SABNZBD_PORT=9200
SABNZBD_APIKEY=--YOUR_API_KEY--
CP_PID=/var/run/couchpotato.pid
CP_CONFIG=/usr/local/couchpotato.config.ini
CP_DATADIR=/root/.couchpotato
@swissmanu
swissmanu / templatePrecompiler.js
Last active December 16, 2015 18:58
Creates precompiled handlebar templates and makes them available as a basic common js module. Change the paths templatesSource and templatesDestination to your needs. Simply execute with "node templatePrecompiler.js"
/** File: Template Preprocessor
* Creates precompiled handlebar templates and makes them available as a simple
* common js module.
*/
var Handlebars = require('handlebars')
, _ = require('underscore')
, path = require('path')
, fs = require('fs')
, templatesSource = path.join(__dirname, 'src', 'templates')
, templatesDestination = path.join(__dirname, 'src', 'templates', 'precompiledTemplates.js')
@swissmanu
swissmanu / create.sh
Last active December 17, 2015 15:09
create.sh creates a git repository which automatically publishs its contents over a webserver when receiving changes from its remotes. It's like gh-pages, but on your own server and without an extra branch.
#!/bin/bash
if [ -z "$1" ]; then
echo "Supply a name for the repo/site to publish"
exit 1;
fi
URLROOT=gitpages.mydomain.com
ROOT=/home/www/gitpages.mydomain.com
REPOS=$ROOT/repositories