Tuesday, October 20, 2009

image cropping

This code is for cropping any image in any particular rectangle.

- (UIImage*) getSmallImageUIImage*) img
{
CGSize size = img.size;
CGFloat ratio = 0;
if (size.width < size.height) {
ratio = 36 / size.width;
} else {
ratio = 36 / size.height;
}
CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);

UIGraphicsBeginImageContext(rect.size);
[img drawInRect:rect];

UIImage *tempImg = [UIGraphicsGetImageFromCurrentImageContext() retain];
UIGraphicsEndImageContext();
return [tempImg autorelease];
}

- (UIImage*)imageByCroppingUIImage *)imageToCrop toRectCGRect)rect
{

//create a context to do our clipping in
UIGraphicsBeginImageContext(rect.size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();

//create a rect with the size we want to crop the image to
//the X and Y here are zero so we start at the beginning of our
//newly created context

CGFloat X = (imageToCrop.size.width - rect.size.width)/2;
CGFloat Y = (imageToCrop.size.height - rect.size.height)/2;


CGRect clippedRect = CGRectMake(X, Y, rect.size.width, rect.size.height);
//CGContextClipToRect( currentContext, clippedRect);



//create a rect equivalent to the full size of the image
//offset the rect by the X and Y we want to start the crop
//from in order to cut off anything before them
CGRect drawRect = CGRectMake(0,
0,
imageToCrop.size.width,
imageToCrop.size.height);

CGContextTranslateCTM(currentContext, 0.0, drawRect.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0);
//draw the image to our clipped context using our offset rect
//CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);


CGImageRef tmp = CGImageCreateWithImageInRect(imageToCrop.CGImage, clippedRect);

//pull the image from our cropped context
UIImage *cropped = [UIImage imageWithCGImage:tmp];//UIGraphicsGetImageFromCurrentImageContext();
CGImageRelease(tmp);
//pop the context to get back to the default
UIGraphicsEndImageContext();

//Note: this is autoreleased*/
return cropped;
}

Wednesday, September 30, 2009

Hiding Unhiding back button from navigation controller

This is how to hide back button from the navigation Item
[self.navigationItem setHidesBackButton:YES]

and this is to unhide...

[self.navigationItem setHidesBackButton:NO]

Wednesday, September 2, 2009

UIPickerView tutorial

Here is a good tutorial of UIPickerView

http://www.iphonesdkarticles.com/2009/01/uipickerview-creating-simple-picker.html

Tuesday, July 21, 2009

Take current screen shot

Code for getting the current screen shot as UIImage

//NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

CGRect screenRect = [[UIScreen mainScreen] bounds];

UIGraphicsBeginImageContext(screenRect.size);

CGContextRef ctx = UIGraphicsGetCurrentContext();

[[UIColor blackColor] set];

CGContextFillRect(ctx, screenRect);

[self.view.layer renderInContext:ctx];

UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();

UIImageWriteToSavedPhotosAlbum(screenImage, nil, nil, nil);

UIGraphicsEndImageContext();

Wednesday, June 24, 2009

Date from string and vice versa

Want to convert a string to date and vice versa, you can use NSDateFormatter for this.

Here is a sample for converting a string to date

NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];

[dateForm setDateFormat:@"yyyy-MM-dd"];

NSDate *dateSelected = [dateForm dateFromString:@"2009-06-25"];

[dateForm release];



Here you can have to modify the date format according to the string you use.


For example:

[dateForm setDateFormat:@"yyyy-MM-dd h:m"];

NSDate *dateSelected = [dateForm dateFromString:@"2009-06-25 11:40"];



You can use the same dateformatter for converting a date to string also. Set the date format you want and use the stringFromDate method instead of dateFromString. Here is an example:


NSDateFormatter *dateForm = [[NSDateFormatter allocinit];

[dateForm setDateFormat:@"EEEE, yyyy-MMM-dd h:m"];

NSString *dateString = [dateForm stringFromDate:[NSDate date]];

[dateForm release];


Instead of this date fromat strings, you can use different styles provided by date formatter


 [dateFormatter setDateStyle:NSDateFormatterMediumStyle];

 [dateFormatter setTimeStyle:NSDateFormatterShortStyle];


Now you want all the string formats that can be used with NSDateFormatter. Here is that


------------------------------------------------------------------------------------------------

a: AM/PM

A: 0~86399999 (Millisecond of Day)

 

c/cc: 1~7 (Day of Week)

ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat

cccc: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday

 

d: 1~31 (0 padded Day of Month)

D: 1~366 (0 padded Day of Year)

 

e: 1~7 (0 padded Day of Week)

E~EEE: Sun/Mon/Tue/Wed/Thu/Fri/Sat

EEEE: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday

 

F: 1~5 (0 padded Week of Month, first day of week = Monday)

 

g: Julian Day Number (number of days since 4713 BC January 1)

G~GGG: BC/AD (Era Designator Abbreviated)

GGGG: Before Christ/Anno Domini

 

h: 1~12 (0 padded Hour (12hr))

H: 0~23 (0 padded Hour (24hr))

 

k: 1~24 (0 padded Hour (24hr)

K: 0~11 (0 padded Hour (12hr))

 

L/LL: 1~12 (0 padded Month)

LLL: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec

LLLL: January/February/March/April/May/June/July/August/September/October/November/December

 

m: 0~59 (0 padded Minute)

M/MM: 1~12 (0 padded Month)

MMM: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec

MMMM: January/February/March/April/May/June/July/August/September/October/November/December

 

q/qq: 1~4 (0 padded Quarter)

qqq: Q1/Q2/Q3/Q4

qqqq: 1st quarter/2nd quarter/3rd quarter/4th quarter

Q/QQ: 1~4 (0 padded Quarter)

QQQ: Q1/Q2/Q3/Q4

QQQQ: 1st quarter/2nd quarter/3rd quarter/4th quarter

 

s: 0~59 (0 padded Second)

S: (rounded Sub-Second)

 

u: (0 padded Year)

 

v~vvv: (General GMT Timezone Abbreviation)

vvvv: (General GMT Timezone Name)

 

w: 1~53 (0 padded Week of Year, 1st day of week = Sunday, NB: 1st week of year starts from the last Sunday of last year)

W: 1~5 (0 padded Week of Month, 1st day of week = Sunday)

 

y/yyyy: (Full Year)

yy/yyy: (2 Digits Year)

Y/YYYY: (Full Year, starting from the Sunday of the 1st week of year)

YY/YYY: (2 Digits Year, starting from the Sunday of the 1st week of year)

 

z~zzz: (Specific GMT Timezone Abbreviation)

zzzz: (Specific GMT Timezone Name)

Z: +0000 (RFC 822 Timezone)

------------------------------------------------------------------------------------------------


Generating a random number


Want to generate a random number in your iphone application?

Just use the arc4random() method.

int random = arc4random() % 10;


This will return a value between 0 and 9. Change value 10 in your program to get random values in different range. 

Tuesday, June 23, 2009

Badge settings

Setting the icon badge number is very easy a one line code in iphone sdk

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:10];


You can use this line anywhere in the project to set the badge number. Another proper way to set the badge number  is when terminating the application. So here is the sample code for that.

/// In application delegate

- (void)applicationWillTerminate:(UIApplication *)application

{

int badgeNumber = [self countBadgeNumber]; // Method to find badge number

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeNumber];

}



Note: If the badge number is 0, no badge will be displayed.