NSString * path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"jpg"];
UIImage * img = [[UIImage alloc]initWithContentsOfFile:path];
CGImageRef image = [img CGImage];
CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(image));
const unsigned char * buffer = CFDataGetBytePtr(data);
Friday, August 19, 2011
Sunday, July 24, 2011
Save in NSUserDefaults
Saving
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];
// saving an NSInteger
[prefs setInteger:42 forKey:@"integerKey"];
// saving a Double
[prefs setDouble:3.1415 forKey:@"doubleKey"];
// saving a Float
[prefs setFloat:1.2345678 forKey:@"floatKey"];
// This is suggested to synch prefs, but is not needed (I didn't put it in my tut)
[prefs synchronize];
Loading
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *myString = [prefs stringForKey:@"keyToLookupString"];
// getting an NSInteger
NSInteger myInt = [prefs integerForKey:@"integerKey"];
// getting an Float
float myFloat = [prefs floatForKey:@"floatKey"];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];
// saving an NSInteger
[prefs setInteger:42 forKey:@"integerKey"];
// saving a Double
[prefs setDouble:3.1415 forKey:@"doubleKey"];
// saving a Float
[prefs setFloat:1.2345678 forKey:@"floatKey"];
// This is suggested to synch prefs, but is not needed (I didn't put it in my tut)
[prefs synchronize];
Loading
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *myString = [prefs stringForKey:@"keyToLookupString"];
// getting an NSInteger
NSInteger myInt = [prefs integerForKey:@"integerKey"];
// getting an Float
float myFloat = [prefs floatForKey:@"floatKey"];
Saturday, July 23, 2011
iOS SDK tips and tricks
Check if string null
if([myStr isEqual: [NSNull null]])
NSLog(@"null");
else
NSLog(@"not null");
=============================================
Crop Image
UIImageView *tempImageview;
CGRect newRect = CGRectMake(0, 0, newWidth, tempImageview.frame.size.height);
CGImageRef tmp = CGImageCreateWithImageInRect([tempImageview.image CGImage], newRect);
newRect = CGRectMake(tempImageview.frame.origin.x, tempImageview.frame.origin.y, newWidth, tempImageview.frame.size.height);
=============================================
Color with RGB
UIColor *myColor = [UIColor colorWithRed:32/255.0 green:93/255.0 blue:137/255.0 alpha:1.0];
tempImageview.frame = newRect;
tempImageview.image = [UIImage imageWithCGImage:tmp];
=============================================
Bring View to the top:
[self.view bringSubviewToFront:myImageView];
Or:
– bringSubviewToFront:
– sendSubviewToBack:
– insertSubview:atIndex:
– insertSubview:aboveSubview:
– insertSubview:belowSubview:
– exchangeSubviewAtIndex:withSubviewAtIndex:
=============================================
add Image Border
#import
[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];
=============================================
Rate this app
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=374892313&onlyLatestVersion=false&type=Purple+Software"]];
Replace id with the application ID from itunesconnect.
=============================================
Show/Hide Keyboard:
Keyboard Show:
[searchBar becomeFirstResponder];
Keyboard hide:
[searchBar resignFirstResponder];
=============================================
auto-lock disable
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
=============================================
Convert string to number
NSString *intString =@"1";
int value = [intString intValue]; //1
floatValue -> 1.00
doubleValue ->1.0000000
=============================================
Random
int lastNumber;
int rNumber;
while(rNumber == lastNumber)
rNumber = arc4random()%10;
lastNumber = rNumber;
=============================================
Save and Read Data
Save data:
NSString *testValue = [[NSUserDefaults standartUserDefaults] stringForKey:@”stringKey”];
if(testValue == nil)
{
//set default value
[[NSUserDefaults standartUserDefaults] setInteger:42 forKey:@”intKey”];
[[NSUserDefaults standartUserDefaults] setObject:@”data” forKey:@”stringKey”];
[[NSUserDefaults standartUserDefaults] setDouble:3.1415 forKey:@”doubleKey”];
[[NSUserDefaults standartUserDefaults] setFloat:1.23 forKey:@”floatKey”];
[[NSUserDefaults standartUserDefaults] synchronize];
}
Read data:
NSString *defaultString = [[NSUserDefaults standartUserDefaults] stringForKey:@”stringKey”];
int defaultInt = [[NSUserDefaults standartUserDefaults] integerForKey:@”intKey”];
//or NSInteger *defaultInt
int defaultFloat = [[NSUserDefaults standartUserDefaults] floatForKey:@”floatKey”];
=============================================
Absolute Value
float floatValue = -123.45678;
NSLog(@"%f", fabsf(floatValue)); //output: 123.45678
=============================================
=============================================
=============================================
=============================================
if([myStr isEqual: [NSNull null]])
NSLog(@"null");
else
NSLog(@"not null");
=============================================
Crop Image
UIImageView *tempImageview;
CGRect newRect = CGRectMake(0, 0, newWidth, tempImageview.frame.size.height);
CGImageRef tmp = CGImageCreateWithImageInRect([tempImageview.image CGImage], newRect);
newRect = CGRectMake(tempImageview.frame.origin.x, tempImageview.frame.origin.y, newWidth, tempImageview.frame.size.height);
=============================================
Color with RGB
UIColor *myColor = [UIColor colorWithRed:32/255.0 green:93/255.0 blue:137/255.0 alpha:1.0];
tempImageview.frame = newRect;
tempImageview.image = [UIImage imageWithCGImage:tmp];
=============================================
Bring View to the top:
[self.view bringSubviewToFront:myImageView];
Or:
– bringSubviewToFront:
– sendSubviewToBack:
– insertSubview:atIndex:
– insertSubview:aboveSubview:
– insertSubview:belowSubview:
– exchangeSubviewAtIndex:withSubviewAtIndex:
=============================================
add Image Border
#import
[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];
=============================================
Rate this app
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=374892313&onlyLatestVersion=false&type=Purple+Software"]];
Replace id with the application ID from itunesconnect.
=============================================
Show/Hide Keyboard:
Keyboard Show:
[searchBar becomeFirstResponder];
Keyboard hide:
[searchBar resignFirstResponder];
=============================================
auto-lock disable
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
=============================================
Convert string to number
NSString *intString =@"1";
int value = [intString intValue]; //1
floatValue -> 1.00
doubleValue ->1.0000000
=============================================
Random
int lastNumber;
int rNumber;
while(rNumber == lastNumber)
rNumber = arc4random()%10;
lastNumber = rNumber;
=============================================
Save and Read Data
Save data:
NSString *testValue = [[NSUserDefaults standartUserDefaults] stringForKey:@”stringKey”];
if(testValue == nil)
{
//set default value
[[NSUserDefaults standartUserDefaults] setInteger:42 forKey:@”intKey”];
[[NSUserDefaults standartUserDefaults] setObject:@”data” forKey:@”stringKey”];
[[NSUserDefaults standartUserDefaults] setDouble:3.1415 forKey:@”doubleKey”];
[[NSUserDefaults standartUserDefaults] setFloat:1.23 forKey:@”floatKey”];
[[NSUserDefaults standartUserDefaults] synchronize];
}
Read data:
NSString *defaultString = [[NSUserDefaults standartUserDefaults] stringForKey:@”stringKey”];
int defaultInt = [[NSUserDefaults standartUserDefaults] integerForKey:@”intKey”];
//or NSInteger *defaultInt
int defaultFloat = [[NSUserDefaults standartUserDefaults] floatForKey:@”floatKey”];
=============================================
Absolute Value
float floatValue = -123.45678;
NSLog(@"%f", fabsf(floatValue)); //output: 123.45678
=============================================
=============================================
=============================================
=============================================
Delete User Default Data
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"dataName"];
Get iPhone Device Name, Unique Device Identifier (UDID), OS and Model
NSLog(@"uniqueIdentifier: %@", [[UIDevice currentDevice] uniqueIdentifier]);
NSLog(@"name: %@", [[UIDevice currentDevice] name]);
NSLog(@"systemName: %@", [[UIDevice currentDevice] systemName]);
NSLog(@"systemVersion: %@", [[UIDevice currentDevice] systemVersion]);
NSLog(@"model: %@", [[UIDevice currentDevice] model]);
NSLog(@"localizedModel: %@", [[UIDevice currentDevice] localizedModel]);
iOS version:
NSLog(@"%@", [[UIDevice currentDevice] systemVersion]);
NSLog(@"name: %@", [[UIDevice currentDevice] name]);
NSLog(@"systemName: %@", [[UIDevice currentDevice] systemName]);
NSLog(@"systemVersion: %@", [[UIDevice currentDevice] systemVersion]);
NSLog(@"model: %@", [[UIDevice currentDevice] model]);
NSLog(@"localizedModel: %@", [[UIDevice currentDevice] localizedModel]);
iOS version:
NSLog(@"%@", [[UIDevice currentDevice] systemVersion]);
Round Number
#import "math.h"
float numberToRound;
int result;
numberToRound = 4.51;
result = (int)roundf(numberToRound);
NSLog(@"roundf(%f) = %d", numberToRound, result); // roundf(4.510000) = 5
result = (int)ceilf(numberToRound);
NSLog(@"ceilf(%f) = %d", numberToRound, result); // ceilf(4.510000) = 5
result = (int)floorf(numberToRound);
NSLog(@"floorf(%f) = %d", numberToRound, result); // floorf(4.510000) = 4
float numberToRound;
int result;
numberToRound = 4.51;
result = (int)roundf(numberToRound);
NSLog(@"roundf(%f) = %d", numberToRound, result); // roundf(4.510000) = 5
result = (int)ceilf(numberToRound);
NSLog(@"ceilf(%f) = %d", numberToRound, result); // ceilf(4.510000) = 5
result = (int)floorf(numberToRound);
NSLog(@"floorf(%f) = %d", numberToRound, result); // floorf(4.510000) = 4
Download file
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http"//webite/"]]
Trimming whitespace from ends of a string
NSString *ook = @"\n \t\t hello there \t\n \n\n";
NSString *trimmed =
[ook stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"trimmed: '%@'", trimmed);
NSString *trimmed =
[ook stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"trimmed: '%@'", trimmed);
Deserializing/Serializing with JSON
Deserializing with TouchJSON
#import "CJSONDeserializer.h"
NSData *jsonData = [NSData dataWithContentsOfFile: path]; // or from network, or whatever
NSError *error;
NSArray *playlists =
[[CJSONDeserializer deserializer] deserializeAsArray: jsonData error: &error];
Serializing with TouchJSON
#import "CJSONSerializer.h"
NSArray *allSongs = [self allSongs];
NSString *jsonString = [[CJSONSerializer serializer] serializeObject: allSongs];
NSData *data = [jsonString dataUsingEncoding: NSUTF8StringEncoding];
// write |data| to a file, send over the network, etc
#import "CJSONDeserializer.h"
NSData *jsonData = [NSData dataWithContentsOfFile: path]; // or from network, or whatever
NSError *error;
NSArray *playlists =
[[CJSONDeserializer deserializer] deserializeAsArray: jsonData error: &error];
Serializing with TouchJSON
#import "CJSONSerializer.h"
NSArray *allSongs = [self allSongs];
NSString *jsonString = [[CJSONSerializer serializer] serializeObject: allSongs];
NSData *data = [jsonString dataUsingEncoding: NSUTF8StringEncoding];
// write |data| to a file, send over the network, etc
Delete A Substring In Objective C, Iphone , Mac
/** initialize two strings **/
NSString *string = @"This is heaven";
NSString *part = @"heaven";
/** Get the range of the substring in the original string **/
NSRange range = [string rangeOfString:part];
/** Delete the substring from the original string **/
[string deleteCharactersInRange:range];
NSString *string = @"This is heaven";
NSString *part = @"heaven";
/** Get the range of the substring in the original string **/
NSRange range = [string rangeOfString:part];
/** Delete the substring from the original string **/
[string deleteCharactersInRange:range];
Distance between 2 points
CGPoint first= CGPointMake(50,50);
CGPoint second= CGPointMake(200,200);
float maxDistance= ccpDistance(first, second);
CGPoint second= CGPointMake(200,200);
float maxDistance= ccpDistance(first, second);
Detect Single/Double/Triple Taps In IPhone
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *urtouch = [touches anyObject];
NSUInteger urtapCount = [urtouch tapCount];
switch (urtapCount) {
case 1:
NSLog(@"Single Tap Detected!");
break;
case 2:
NSLog(@"Double Tap Detected!");
break;
case 3:
NSLog(@"Triple Tap Detected!");
break;
default :
break;
}
}
UITouch *urtouch = [touches anyObject];
NSUInteger urtapCount = [urtouch tapCount];
switch (urtapCount) {
case 1:
NSLog(@"Single Tap Detected!");
break;
case 2:
NSLog(@"Double Tap Detected!");
break;
case 3:
NSLog(@"Triple Tap Detected!");
break;
default :
break;
}
}
iPhone SDK examples and resources
http://www.iphoneexamples.com/
http://borkware.com/quickies/everything-by-date
http://borkware.com/quickies/everything-by-date
screenshot programmatically on iPhone
#import
...
UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
...
UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
Copy Image to clipboard
// copy to clipboard
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSData *data = [NSData dataWithContentsOfFile:filePath];
[pasteboard setData:data forPasteboardType:@"public.jpeg"];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSData *data = [NSData dataWithContentsOfFile:filePath];
[pasteboard setData:data forPasteboardType:@"public.jpeg"];
Sunday, March 20, 2011
Get files from directory (including all subdirectories)
string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.bmp", SearchOption.AllDirectories);
Subscribe to:
Posts (Atom)