Skip to content

Instantly share code, notes, and snippets.

View jacksonfdam's full-sized avatar
💻
Coding...

Jackson F. de A. Mafra jacksonfdam

💻
Coding...
View GitHub Profile
@jacksonfdam
jacksonfdam / gist:2819465
Created May 28, 2012 14:31
Fetching Remote Images
dispatch_queue_t img_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(img_queue, ^{
NSURL *url = [NSURL URLWithString:@"http://imgs.xkcd.com/comics/formal_logic.png"];
NSError *error = nil;
NSData *image = [NSData dataWithContentsOfURL:url options:0 error:&error];
if(!error && image && [image length] > 0) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"image.png"];
@jacksonfdam
jacksonfdam / gist:2819485
Created May 28, 2012 14:39
iOS SDK: NSNotification
Ordering from a food cart is a lot like working with an NSNotification. You walk up to the counter, place your order, get a number, and wait for your number to be called. You usually stand around with five other people who are waiting for their number to be called, too. And when the chef is finished preparing your meal, the person behind the counter calls your number, and you sit down to eat. With NSNotification, you become an observer for "your number," and when the object posting the notification is done "making your food," NSNotificationCenter calls your number so you can come get your "food". In this tutorial, instead of waiting for food, we're going to wait for the device to rotate and then send the current orientation to the observer. We'll talk about how to register to receive a notification, post a notification, and pass a string object along with the notification using userInfo.
Set Up Your Project
Launch Xcode and click File > New > Project. Select an iOS Single View Application and click "Next".
@jacksonfdam
jacksonfdam / createDocumentSubdir.m
Created May 28, 2012 17:20 — forked from slashingweapon/createDocumentSubdir.m
Useful Objective-C Snippets
// Create a path to a directory, and make sure it exists.
// The iOS documentation will tell you that URLs are preferred, but there is no createDirectory method for
// URLs. So we have to use string paths instead.
// Create a sub-directory in the application's Documents directory. Return the path on success, or nil on failure.
- (void)createDocumentSubdirectory:(NSString*)dirName {
NSString *retval = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dirPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:dirName];
NSFileManager *fm = [[NSFileManager alloc] init];
@jacksonfdam
jacksonfdam / autoscoped.h
Created May 28, 2012 17:21 — forked from mkhl/autoscoped.h
Objective-C macros
// GCC Attribute for autoscoped Obj-C objects
// Source: http://www.cocoabuilder.com/archive/message/cocoa/2009/3/13/232287
#define autoscoped __attribute__((cleanup(releaseObject)))
static inline void releaseObject(id *object)
{
[*object release];
}
- (void)test
{
int foo = 1;
void (^block)() = ^() {
NSLog(@"%d at %p", foo, &foo); //=> 1 at 0xbfffd65c / エンクロージャの変数fooは参照できる.
// 以下は 'Variable is not assignable' コンパイルエラー
// foo = 2;
};
block();
NSLog(@"%d at %p", foo, &foo); //=> 1 at 0xbfffd6ac / block内のfooとはアドレスが違う.
@jacksonfdam
jacksonfdam / mobile-meta-links.html
Created May 28, 2012 17:23
iOS Web App Configuration
@jacksonfdam
jacksonfdam / hideaddrbar.js
Created May 28, 2012 17:23 — forked from scottjehl/hideaddrbar.js
Normalized hide address bar for iOS & Android
/*
* Normalized hide address bar for iOS & Android
* (c) Scott Jehl, scottjehl.com
* MIT License
*/
(function( win ){
var doc = win.document;
// If there's a hash, or addEventListener is undefined, stop here
if( !location.hash && win.addEventListener ){
// Original code from http://www.blog.highub.com/mobile-2/a-fix-for-iphone-viewport-scale-bug/
var metas = document.getElementsByTagName('meta');
var i;
if (navigator.userAgent.match(/iPhone/i)) {
for (i=0; i<metas.length; i++) {
if (metas[i].name == "viewport") {
metas[i].content = "width=device-width, minimum-scale=1.0, maximum-scale=1.0";
}
}
// Original code from http://www.blog.highub.com/mobile-2/a-fix-for-iphone-viewport-scale-bug/
var metas = document.getElementsByTagName('meta');
var i;
if (navigator.userAgent.match(/iPhone/i)) {
for (i=0; i<metas.length; i++) {
if (metas[i].name == "viewport") {
metas[i].content = "width=device-width, minimum-scale=1.0, maximum-scale=1.0";
}
}
@jacksonfdam
jacksonfdam / UntitledViewController.h
Created May 28, 2012 17:52
How to implement UIScrollView with 1000+ subviews?
@interface UntitledViewController : UIViewController <UIScrollViewDelegate>
{
@private
UIScrollView *_scrollView;
NSArray *_objects;
int refPage, currentPage;
UILabel *_detailLabel1;
UILabel *_detailLabel2;
UILabel *_detailLabel3;