Skip to content

Instantly share code, notes, and snippets.

@zoul
zoul / AVAssetExportSession+Testing.m
Created November 8, 2010 08:35
Export assets synchronously. Good for testing.
@implementation AVAssetExportSession (Testing)
- (void) exportSynchronously
{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[self exportAsynchronouslyWithCompletionHandler:^{
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_release(semaphore);
@zoul
zoul / OSStatus.m
Created October 19, 2010 08:21
Decode OSStatus error codes into strings.
NSString *NSStringFromOSStatus(OSStatus errCode)
{
if (errCode == noErr)
return @"noErr";
char message[5] = {0};
*(UInt32*) message = CFSwapInt32HostToBig(errCode);
return [NSString stringWithCString:message encoding:NSASCIIStringEncoding];
}
@zoul
zoul / UIImage+ForceLoad.m
Created September 14, 2010 07:06
Forces UIImage to load and decode its data
@implementation UIImage (ForceLoading)
- (void) forceLoad
{
const CGImageRef cgImage = [self CGImage];
const int width = CGImageGetWidth(cgImage);
const int height = CGImageGetHeight(cgImage);
const CGColorSpaceRef colorspace = CGImageGetColorSpace(cgImage);
@zoul
zoul / DownloadOperation.h
Created September 14, 2010 06:36
Asynchronous NSURLConnection in concurrent NSOperation
@interface DownloadOperation : NSOperation
{
NSURLRequest *request;
NSURLConnection *connection;
NSMutableData *receivedData;
}
@property(readonly) BOOL isExecuting;
@property(readonly) BOOL isFinished;
@zoul
zoul / UINavigationItemPlus.h
Created August 31, 2010 07:40
Multiple buttons in UINavigationItem
@interface UINavigationItem (Extensions)
- (void) setRightBarButtonItemsWithTotalWidth: (NSUInteger) width
items: (UIBarItem*) firstItem, ...;
@end
@zoul
zoul / UIDevice+Model.h
Created March 20, 2010 10:14
Detect specific iPhone/iPod/iPad model
typedef enum {
kDeviceTypeUnknown = 0,
kDeviceTypeSimulator,
kDeviceTypeiPhone1G,
kDeviceTypeiPhone3G,
kDeviceTypeiPhone3GS,
kDeviceTypeiPhone4,
kDeviceTypeiPod1G,
kDeviceTypeiPod2G,
kDeviceTypeiPad
@zoul
zoul / objc-varargs.m
Created March 19, 2010 13:36
Variable arguments in Objective-C
- (void) processStrings: (NSString*) str1, ...
{
va_list args;
va_start(args, str1);
NSString *arg = str1;
while (arg != nil) {
// do something with arg
arg = va_arg(args, NSString*);
}
va_end(args);
@zoul
zoul / crawler.pl
Created January 5, 2010 18:48
Simple web crawler in Perl
#!/usr/bin/perl
use Modern::Perl;
use WWW::Mechanize;
my $root = 'http://naima:3000/cs/';
my $domain = 'http://naima';
my $mech = WWW::Mechanize->new;
sub visit {
@zoul
zoul / rss-filter.pl
Created December 21, 2009 21:02
Filter RSS using an XSL template
#!/usr/bin/perl
use Modern::Perl;
use XML::RSS::Tools;
use Perl6::Slurp;
my $style = slurp \*DATA;
my $feed = XML::RSS::Tools->new;
#$feed->set_http_client('lite');
#$feed->debug(1);
@zoul
zoul / Sound.h
Created October 9, 2009 08:09
Simple wrapper around Audio Services
/*
- Trivial wrapper around system sound as provided by Audio Services.
- Don’t forget to link against the Audio Toolbox framework.
- Assumes ARC support.
*/
@interface Sound : NSObject
// Path is relative to the resources dir.
- (id) initWithPath: (NSString*) path;