Skip to content

Instantly share code, notes, and snippets.

View benvium's full-sized avatar

Ben Clayton benvium

  • www.calvium.com
  • Bristol, UK
View GitHub Profile
@benvium
benvium / resizer.txt
Last active September 3, 2018 13:30 — forked from gingertom/resizer.sh
Resize all @3x images in a folder to @2x and @1x sizes. Using this script you need only ever edit the @3x images. Drop this script into the folder containing your images and double-click it in Finder to run. Note it will destructively overwrite the @2x and @1x-sized images, so be careful. Don't run this script in a folder that's not under versio…
#!/bin/bash -e
# Ensure we're running in location of script.
cd "`dirname $0`"
for f in *; do
if [[ $f == *@3x* ]];
then
echo "$f -> ${f//@3x/@2x}, ${f//@3x/}"
convert "$f" -resize 66.66666% "${f//@3x/@2x}"
@benvium
benvium / hasRunThisVersion.mm
Created April 17, 2013 17:06
iPhone / iOS: Detect if user has run this version before
// Warn if this is the first run of this new version
NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
NSString* version = [infoDict objectForKey:@"CFBundleVersion"];
NSString* defaultsKey = [NSString stringWithFormat:@"hasRunVersion%@", version];
BOOL hasRunThisVersion = [[NSUserDefaults standardUserDefaults] boolForKey:defaultsKey];
Log(@"hasRunThisVersion is %d", hasRunThisVersion);
if (!hasRunThisVersion) {
// Do things you want to happen only once
@benvium
benvium / DetectBackButtonPressInUINavigationController.mm
Created April 17, 2013 12:04
Code to put into a UIViewController to detect when (a) the BACK button is pressed or (b) the view is popped off the stack for whatever reason.
-(void)viewWillDisappear:(BOOL)animated {
NSUInteger ind = [[self.navigationController viewControllers] indexOfObject:self];
if (ind == NSNotFound) {
<#code to handle back#>
}
}
@benvium
benvium / nameFromAudio.js
Created April 15, 2013 09:00
Code for the bonus section of AppFurnace Tutorial 7
//========================================================================
// Add this bit in, one line for each audio file
//========================================================================
var nameToAudio = {
"audio/story1.mp3":"What I had for breakfast",
"audio/story2.mp3":"What'll have for dinner"
};
// This function returns the track description if there is one, otherwise returns the name of the audio file.
function nameFromAudio(audio) {
@benvium
benvium / LoadJSONFile.js
Created April 12, 2013 09:14
AppFurnace: Utility function to load data from a .json file in the Files tab
/**
* Utility function to load JSON file a .json file in the Files tab.
*
* Params:
* @filename - the filename to use. Use the path to the file, e.g. "data.json" for a file in the root, or "data/content.json" for a file in a folder called content.
* @callback - a function to call with the data when it's loaded. You'll need to provide a function with a parameter called 'data'/. e.g. function (data) { popup("I've got data!"); }
*
* Example:
*
* // Load a file called data.json....
// GPS Audio Player Example v1
init();
/////////////////////////////////////////////////////////////////////////////////////
// Audio Channel Setup
/////////////////////////////////////////////////////////////////////////////////////
// When app starts, create two channels and setup 'watches' (so we know what the channels are doing)
function init() {
af.audioChannel.init(2);
var ua = navigator.userAgent;
if( ua.indexOf("Android") >= 0 )
{
var androidversion = parseFloat(ua.slice(ua.indexOf("Android")+8));
if (androidversion < 2.3)
{
...
}
}
@benvium
benvium / renumberpng.sh
Created March 8, 2013 13:56
Rename all PNG files in the current directory to have numbers e.g. 0001.png 0002.png.
#!/bin/sh
a=1
for i in *.png; do
new=$(printf "%04d.png" "${a}") #04 pad to length of 4
mv "${i}" "${new}"
let a=a+1
done
@benvium
benvium / gist:5049139
Created February 27, 2013 16:16
Animated replacement of self.navigationItem.rightBarButtonItem with an animated spinner, e.g. when network is busy. Sadly getting the layout right is hard - the width of the rightbarbuttonitem comes back as 0...
UIActivityIndicatorView* spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
spinner.hidesWhenStopped = YES;
[spinner startAnimating];
UIView* view = [[UIView alloc] initWithFrame:CGRectZero];
view.frame = CGRectMake(0, 0, 20, 30);
[view addSubview:spinner];
spinner.center = CGPointMake(width/2, 15);
@benvium
benvium / writeUIImageToDocs.mm
Created January 13, 2013 09:02
Write UIImage to documents directory
// UIImage called 'image'
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"foo.png"];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToURL:[NSURL fileURLWithPath:filePath] atomically:YES];
NSLog(@"written to %@", filePath);