Tag Archive for iphone

Add a button with background image

Button];
[super viewDidLoad];
}
source

UIImage resize

UIImage* resizedImage(UIImage *inImage, CGRect thumbRect)
{
CGImageRef			imageRef = [inImage CGImage];
CGImageAlphaInfo	alphaInfo = CGImageGetAlphaInfo(imageRef);

// There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate
// see Supported Pixel Formats in the Quartz 2D Programming Guide
// Creating a Bitmap Graphics Context section
// only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst,
// and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported
// The images on input here are likely to be png or jpeg files
if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;

// Build a bitmap context that's the size of the thumbRect
CGContextRef bitmap = CGBitmapContextCreate(
NULL,
thumbRect.size.width,		// width
thumbRect.size.height,		// height
CGImageGetBitsPerComponent(imageRef),	// really needs to always be 8
4 * thumbRect.size.width,	// rowbytes
CGImageGetColorSpace(imageRef),
alphaInfo
);

// Draw into the context, this scales the image
CGContextDrawImage(bitmap, thumbRect, imageRef);

// Get an image from the context and a UIImage
CGImageRef	ref = CGBitmapContextCreateImage(bitmap);
UIImage*	result = [UIImage imageWithCGImage:ref];

CGContextRelease(bitmap);	// ok if NULL
CGImageRelease(ref);

return result;
}

source

Creating a UILabel dynamically

UILabel *scoreLabel = [ [UILabel alloc ] initWithFrame:CGRectMake((self.bounds.size.width / 2), 0.0, 150.0, 43.0) ];
scoreLabel.textAlignment =  UITextAlignmentCenter;
scoreLabel.textColor = [UIColor whiteColor];
scoreLabel.backgroundColor = [UIColor blackColor];
scoreLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(36.0)];
[self addSubview:scoreLabel];
scoreLabel.text = [NSString stringWithFormat: @"%d", score];

source

detect iphone browser

<?

function is_iPhone($agent='') {
if(empty($agent)) $agent = $_SERVER['HTTP_USER_AGENT'];
if(!empty($agent) and preg_match("~Mozilla/[^ ]+ ((iPhone|iPod); U; CPU [^;]+ Mac OS X; [^)]+) AppleWebKit/[^ ]+ (KHTML, like Gecko) Version/[^ ]+ Mobile/[^ ]+ Safari/[^ ]+~",$agent,$match)) {
return "YES";
} elseif(stristr($agent,'iphone') or stristr($agent,'ipod')){
return "MAYBE";
} else {
return "NO";
}
}

echo is_iPhone();
?>

source

How To Connect To A SOCKS Proxy From An Unjailbroken iPhone/iPod Touch

function FindProxyForURL(url, host) {
return "SOCKS 192.168.xx.xx:yyyy";
}

source

Simple Version

//  NOTE: You need to import the AudioToolbox for access to the vibrate
#import <AudioToolbox/AudioToolbox.h>

//  The one-liner:
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);

//  The function:
- (void)vibrate {
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
}

//  The call from within another method in the same class:
- (void)myMethod {
[self vibrate];
}

source

Stub for SQLite operations for IPhone SDK

(void) aDBOperation: (NSString*)rName  phone:(NSString*) rPhone type:(NSString*)rType
{
if(sqlite3_open([[self dataFilePath] UTF8String],&database) != SQLITE_OK){
sqlite3_close(database);
NSAssert(0, @"Failed to load database");
}

NSString* query = [NSString stringWithFormat:@"INSERT QUERY HERE;", param];
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK){

}
sqlite3_finalize(statement);
NSLog(@"length of returned array %d",[dishes count]);
}
else{
NSLog(@"There was an error loading from db");
}

}

source

iPhone/iPod Touch detection

if(window.navigator.userAgent.match(/(CPU iPhone OS)/))
document.write('This is a iPhone or iPod Touch!');
else
document.write('This is NOT a iPhone or iPod Touch!');

source

Using UIPickerView

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [foodTypes count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [foodTypes objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//NSLog(@"Selected Color: %@. Index of selected color: %i", [foodTypes objectAtIndex:row], row);
}

source

Sorting an NSArray with raw NSString objects

NSArray* allResteraunts = [[NSArray alloc] initWithObjects:@"Peiking", @"AHotel", @"BHotel", nil];
NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES selector:@selector(localizedCompare:)];
NSArray *sortedArray = [allResteraunts sortedArrayUsingDescriptors:[NSArray arrayWithObject:desc]];
[desc release];

source