- (void)viewDidLoad
{
	[super viewDidLoad];

	// set background image of navigation bar
	[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bar_navi.png"] forBarMetrics:UIBarMetricsDefault];

	// left bar button
	UIButton *leftButtonView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 42, 34)];
	[leftButtonView setBackgroundImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
	[leftButtonView addTarget:self action:@selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
	self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButtonView];

	// right bar button
	UIButton *rightButtonView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 42, 34)];
	[rightButtonView setBackgroundImage:[UIImage imageNamed:@"btn_setting.png"] forState:UIControlStateNormal];
	[rightButtonView addTarget:self action:@selector(settingButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
	self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButtonView];
}

If you want to replace NavigationBar’s back button, you must implement the back button functionality.

-(void)backButtonClicked:(id)sender
{
	[self.navigationController popViewControllerAnimated:YES];
}

1. Using C standard library

int getDayOfWeek(int year, int month, int day)
{
	struct tm *pResultTime;
	struct tm targetTime = { 0, 0, 0, day, month-1, year - 1900 };
	time_t targetSec = mktime(&targetTime);

	pResultTime = localtime(&targetSec);

	return pResultTime->tm_wday;
}

2. Using Cocoa frameworks

* Reference: NSDateComponents Class Reference

- (int)getDayOfWeek:(int)year month:(int)month day:(int)day
{
	NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
	[dateComponents setYear:year];
	[dateComponents setMonth:month];
	[dateComponents setDay:day];

	NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
	NSDate *date = [gregorian dateFromComponents:dateComponents];
	NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];

	return [weekdayComponents weekday] - 1;
}

3. Using Zeller’s algorithm

* Reference: Zeller’s congruence

int getDayOfWeek2(int year, int month, int day)
{
	if(month 		year--;
		month += 12;
	}

	int year1 = year/100;
	int year2 = year%100;

	int weekDay =  (day + 26*(month+1)/10 + year2 + year2/4 + year1/4 - year1*2) % 7 - 1;
	if (weekDay < 0) {
		weekDay += 7;
	}

	return weekDay;
}

* Reference: Determination of the day of the week # Sakamoto’s method

int dow(int y, int m, int d)
{
	static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
	y -= m < 3;

	return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
04. May 2012 · 2 comments · Categories: iOS · Tags: , , ,

This static method returns UIColor object converted from Hex value(e.g. 0xF8C701).

+ (UIColor *)getColorFromHex:(int)val
{
	CGFloat red, blue, green;

	red = ((val>>16)&0xff)/255.0;
	green = ((val>>8)&0xff)/255.0;
	blue = (val&0xff)/255.0;

	return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}

1. Create a new UIViewController subclass

Create UIViewController subclass, I named it TestViewController here, and Add the following code to AppDelegate.

* AppDelegate.h

@interface TestViewController : UIViewController
{
    UIImageView *imageView;
}

- (void)buttonClicked:(id)sender;

@end

* AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
	self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	self.window.backgroundColor = [UIColor whiteColor];

	testViewController = [[TestViewController alloc] init];
	[self.window addSubview:testViewController.view];
	[self.window makeKeyAndVisible];

	return YES;
}

You must need ‘AssetsLibrary.framework’ and ‘MobileCoreServices.framework’. Add the frameworks into your project.

2. Modify TestViewController

TestViewController.h

@interface TestViewController : UIViewController
{
	UIImageView *imageView;
}

- (void)buttonClicked:(id)sender;

@end

TestViewController.m

- (void)buttonClicked:(UIButton *)sender
{
	UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

	imagePicker.sourceType = (sender.tag == 1) ? UIImagePickerControllerSourceTypePhotoLibrary : UIImagePickerControllerSourceTypeCamera;
	if (sender.tag == 2) { // Camera
		NSString *requiredMediaType = (NSString *)kUTTypeImage;
		imagePicker.mediaTypes = [NSArray arrayWithObject:requiredMediaType];
	}
	imagePicker.delegate = self;
	self presentModalViewController:imagePicker animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
	UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
	imageView.image = img;

	[picker dismissModalViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
	[super viewDidLoad];

	imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 300, 390)];
	imageView.backgroundColor = [UIColor redColor];
	[self.view addSubview:imageView];

	UIButton *pickButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[pickButton setTitle:@"Pick" forState:UIControlStateNormal];
	pickButton.frame = CGRectMake(50, 410, 100, 40);
	pickButton.tag = 1;
	[pickButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:pickButton];

	if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
		UIButton *takeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
		[takeButton setTitle:@"Take" forState:UIControlStateNormal];
		takeButton.frame = CGRectMake(170, 410, 100, 40);
		takeButton.tag = 2;
		[takeButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
		[self.view addSubview:takeButton];
	}
}

if you want to save the image from camera to Photo Library, add following code in ‘imagePickerController:didFinishPickingMediaWithInfo:’ method.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
	UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
	imageView.image = img;

	ALAssetsLibraryWriteImageCompletionBlock completeBlock = ^(NSURL *assetURL, NSError *error) {
		if (error == nil) {
			NSLog(@"Image Path:%@", [assetURL absoluteString]);
		}
	};

	NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];
	if (url == nil) {
		ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
		[library writeImageToSavedPhotosAlbum:[img CGImage] orientation:(ALAssetOrientation)[img imageOrientation] completionBlock:completeBlock];
	}

	[picker dismissModalViewControllerAnimated:YES];
}