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
=============================================

=============================================

=============================================

=============================================

No comments: