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 / imageCapture.js
Created September 27, 2012 09:24
AppFurnace: Capture Image from Camera and display on-screen.
//------------------------------------------------------------------------------------------------------------------------------
// Call this function on a button press. The image will be displayed on an Image widget with code name 'ui.photo'
//------------------------------------------------------------------------------------------------------------------------------
function takePhoto() {
navigator.camera.getPicture(photoSuccess, photoFail, { quality: 15, destinationType : 0 });
}
// Called when taking a photo works, resizes it and puts it up on the screen for the user.
// Then grabs that as a string and stores it for later uploading.
function photoSuccess(imageString) {
@benvium
benvium / FadeSplash.m
Created September 28, 2012 09:05
iOS: Fade from the splash screen to your initial view. Works on iPhone 5 (and 4,3). Add a UIImageView property called splashView.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//.. do other setup
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
// Transition neatly from splash screen
// Very odd, on iPhone 5 you need to position the splash screen differently..
@benvium
benvium / cameraCapture.js
Created November 1, 2012 11:56
AppFurnace: Capture Image from Camera and display on-screen.
//------------------------------------------------------------------------------------------------------------------------------
// Call this function on a button press. The image will be displayed on an Image widget with code name 'ui.photo'
//------------------------------------------------------------------------------------------------------------------------------
function takePhoto() {
navigator.camera.getPicture(photoSuccess, photoFail, { quality: 15, destinationType : 0 });
}
// Called when taking a photo works, resizes it and puts it up on the screen for the user.
// Then grabs that as a string and stores it for later uploading.
function photoSuccess(imageString) {
@benvium
benvium / restkit-rootarray.m
Created November 13, 2012 18:03
RestKit object mapping when the root element is an array, and we use blocks
// e.g. server returns [ {name:"foo"}, {name:"bar} ]
_objectManager = [RKObjectManager managerWithBaseURLString:@"http://example.com/api"];
RKObjectMapping *rootMapping = [RKObjectMapping mappingForClass:[MyItem class]];
[rootMapping mapKeyPath:@"name" toAttribute:@"name"];
[_objectManager.mappingProvider addObjectMapping:rootMapping]; // note this just stores our instance..
/// LATER ON
@benvium
benvium / RestKitPost.m
Created November 28, 2012 11:29
Performing a POST in RestKit without all the fiddly object mapping stuff
NSDictionary* dict = @{@"Status": newStatus ? @"On" : @"Off"};
NSString* url = @"/api/status";
[[RKClient sharedClient] post:url usingBlock:^(RKRequest *request) {
[request setBody:dict forMIMEType:RKMIMETypeJSON];
request.onDidLoadResponse = ^(RKResponse* response) {
NSLog(@"status ok %@", response);
if (completion) {
@benvium
benvium / example.h
Created November 29, 2012 13:21
Adding and catching links in a UILabel on iOS5 when you're designing with Interface Builder / Storyboards.
#import "TTTAttributedLabel.h"
@interface CVSignupViewController : UITableViewController <TTTAttributedLabelDelegate> {
IBOutlet TTTAttributedLabel* _legalLabel;
}
@end
@benvium
benvium / example.m
Created November 29, 2012 14:12
Auto-tab between UITextFields with maximum numbers of chars (e.g. for a telephone number 000-222-3333)
// In IB, set up your UITextFields with tags 1,2,3
// Set the 'delegate' for each UITextField to be the main ViewController
// Set your ViewController to implement UITextFieldDelegate
// Add function below to your ViewController - use the values in the switch statement to limit the number of chars allowed in each UITextField
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
int maxSize;
switch (textField.tag) {
@benvium
benvium / sortedArrayUsingComparator.m
Created December 3, 2012 11:12
Example of using sortedArrayUsingComparator. idsInOrder has the order that the objects should be in. Actual objects may or may not be in the ordered list.
NSArray* idsInOrder = @[@"foo", @"bar", @"baz", @"badgers"];
NSArray* loadedStuff = @[@{@"id":@"baz"}, @{@"id":@"badgers"}, @{@"id":@"wow"}, @{@"id":@"foo"}];
NSLog(@"UNORDERED: %@", loadedStuff);
NSArray* sorted = [loadedStuff sortedArrayUsingComparator:^NSComparisonResult(NSDictionary* obj1, NSDictionary* obj2) {
// sort by position of id string in order array..
NSUInteger obj1Pos = [idsInOrder indexOfObject:obj1[@"id"] ];
@benvium
benvium / offsetTableViewWhenKeyboardShown.mm
Created January 10, 2013 16:38
When the keyboard appears, scroll the tableView up a pre-defined amount, so the content is nicely centered. I find this useful on a login page.
//-----------------------------------------------------
#pragma mark - When the keyboard appears, scroll the tableView up a pre-defined amount, so the content is nicely centered.
//-----------------------------------------------------
// http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present
// Call this is viewDidLoad
- (void) setupKeyboard {
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
@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);