<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">

<channel>
	<title>CozyDev</title>
	
	<link>http://cozydev.com</link>
	<description>zzerr's Tiny Code Storage</description>
	<lastBuildDate>Thu, 10 May 2012 01:02:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Cozydev" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="cozydev" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>iOS – Customize UINavigationBar’s background and buttons.</title>
		<link>http://cozydev.com/?p=120</link>
		<comments>http://cozydev.com/?p=120#comments</comments>
		<pubDate>Thu, 10 May 2012 00:56:40 +0000</pubDate>
		<dc:creator>zzerr</dc:creator>
				<category><![CDATA[iOS]]></category>
		<category><![CDATA[customizing]]></category>
		<category><![CDATA[UIBarButtonItem]]></category>
		<category><![CDATA[UINavigatoinBar]]></category>

		<guid isPermaLink="false">http://cozydev.com/?p=120</guid>
		<description><![CDATA[If you want to replace NavigationBar&#8217;s back button, you must implement the back button functionality.]]></description>
			<content:encoded><![CDATA[<pre class="brush: objc; title: ; notranslate">
- (void)viewDidLoad
{
	[super viewDidLoad];

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

	// left bar button
	UIButton *leftButtonView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 42, 34)];
	[leftButtonView setBackgroundImage:[UIImage imageNamed:@&quot;btn_back.png&quot;] 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:@&quot;btn_setting.png&quot;] forState:UIControlStateNormal];
	[rightButtonView addTarget:self action:@selector(settingButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
	self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButtonView];
}
</pre>
<p>If you want to replace NavigationBar&#8217;s back button, you must implement the back button functionality. </p>
<pre class="brush: objc; title: ; notranslate">
-(void)backButtonClicked:(id)sender
{
	[self.navigationController popViewControllerAnimated:YES];
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://cozydev.com/?feed=rss2&amp;p=120</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iOS – Get the day of the week</title>
		<link>http://cozydev.com/?p=98</link>
		<comments>http://cozydev.com/?p=98#comments</comments>
		<pubDate>Fri, 04 May 2012 15:53:06 +0000</pubDate>
		<dc:creator>zzerr</dc:creator>
				<category><![CDATA[iOS]]></category>
		<category><![CDATA[a day of the week]]></category>
		<category><![CDATA[date]]></category>

		<guid isPermaLink="false">http://cozydev.com/?p=98</guid>
		<description><![CDATA[1. Using C standard library 2. Using Cocoa frameworks * Reference: NSDateComponents Class Reference 3. Using Zeller&#8217;s algorithm * Reference: Zeller&#8217;s congruence * Reference: Determination of the day of the week # Sakamoto&#8217;s method]]></description>
			<content:encoded><![CDATA[<h3>1. Using C standard library</h3>
<pre class="brush: cpp; title: ; notranslate">
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(&amp;targetTime);

	pResultTime = localtime(&amp;targetSec);

	return pResultTime-&gt;tm_wday;
}
</pre>
<p></p>
<h3>2. Using Cocoa frameworks</h3>
<p>* Reference: <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/Reference/Reference.html">NSDateComponents Class Reference</a></p>
<pre class="brush: objc; title: ; notranslate">
- (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;
}
</pre>
<p></p>
<h3>3. Using Zeller&#8217;s algorithm</h3>
<p>* Reference: <a href="http://en.wikipedia.org/wiki/Zeller's_congruence">Zeller&#8217;s congruence</a></p>
<pre class="brush: cpp; title: ; notranslate">
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 &lt; 0) {
		weekDay += 7;
	}

	return weekDay;
}
</pre>
<p>* Reference: <a href="http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Implementation-dependent_methods_of_Sakamoto.2C_Lachman.2C_Keith_and_Craver">Determination of the day of the week # Sakamoto&#8217;s method</a></p>
<pre class="brush: cpp; title: ; notranslate">
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 &lt; 3;

	return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://cozydev.com/?feed=rss2&amp;p=98</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iOS – Convert a hex value to a UIColor object</title>
		<link>http://cozydev.com/?p=91</link>
		<comments>http://cozydev.com/?p=91#comments</comments>
		<pubDate>Fri, 04 May 2012 08:19:37 +0000</pubDate>
		<dc:creator>zzerr</dc:creator>
				<category><![CDATA[iOS]]></category>
		<category><![CDATA[Color]]></category>
		<category><![CDATA[Hex]]></category>
		<category><![CDATA[RGB]]></category>
		<category><![CDATA[UIColor]]></category>

		<guid isPermaLink="false">http://cozydev.com/?p=91</guid>
		<description><![CDATA[This static method returns UIColor object converted from Hex value(e.g. 0xF8C701).]]></description>
			<content:encoded><![CDATA[<p>This static method returns UIColor object converted from Hex value(e.g. 0xF8C701).</p>
<pre class="brush: objc; title: ; notranslate">
+ (UIColor *)getColorFromHex:(int)val
{
	CGFloat red, blue, green;

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

	return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://cozydev.com/?feed=rss2&amp;p=91</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>iOS – Get an image from camera or photo library</title>
		<link>http://cozydev.com/?p=83</link>
		<comments>http://cozydev.com/?p=83#comments</comments>
		<pubDate>Thu, 03 May 2012 07:41:28 +0000</pubDate>
		<dc:creator>zzerr</dc:creator>
				<category><![CDATA[iOS]]></category>
		<category><![CDATA[Camera]]></category>
		<category><![CDATA[Image]]></category>
		<category><![CDATA[iOS5]]></category>
		<category><![CDATA[Photo Library]]></category>

		<guid isPermaLink="false">http://cozydev.com/?p=83</guid>
		<description><![CDATA[1. Create a new UIViewController subclass Create UIViewController subclass, I named it TestViewController here, and Add the following code to AppDelegate. * AppDelegate.h * AppDelegate.m You must need &#8216;AssetsLibrary.framework&#8217; and &#8216;MobileCoreServices.framework&#8217;. Add the frameworks into your project. 2. Modify TestViewController TestViewController.h TestViewController.m if you want to save the image from camera to Photo Library, add [...]]]></description>
			<content:encoded><![CDATA[<h3>1. Create a new UIViewController subclass</h3>
<p>Create UIViewController subclass, I named it TestViewController here, and Add the following code to AppDelegate.</p>
<p><span style="color: #008080;"><strong>* AppDelegate.h</strong></span></p>
<pre class="brush: objc; title: ; notranslate">
@interface TestViewController : UIViewController
{
    UIImageView *imageView;
}

- (void)buttonClicked:(id)sender;

@end
</pre>
<p><strong><span style="color: #008080;">* AppDelegate.m</span></strong></p>
<pre class="brush: objc; title: ; notranslate">
- (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;
}
</pre>
<blockquote><p>You must need &#8216;AssetsLibrary.framework&#8217; and &#8216;MobileCoreServices.framework&#8217;. Add the frameworks into your project.</p></blockquote>
<h3>2. Modify TestViewController</h3>
<p><span style="color: #008080;"><strong>TestViewController.h</strong></span></p>
<pre class="brush: objc; title: ; notranslate">
@interface TestViewController : UIViewController
{
	UIImageView *imageView;
}

- (void)buttonClicked:(id)sender;

@end
</pre>
<p><strong><span style="color: #008080;">TestViewController.m</span></strong></p>
<pre class="brush: objc; title: ; notranslate">
- (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:@&quot;Pick&quot; 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:@&quot;Take&quot; forState:UIControlStateNormal];
		takeButton.frame = CGRectMake(170, 410, 100, 40);
		takeButton.tag = 2;
		[takeButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
		[self.view addSubview:takeButton];
	}
}
</pre>
<p><span style="color: #666699;"><strong><em>if you want to save the image from camera to Photo Library, add following code in &#8216;imagePickerController:didFinishPickingMediaWithInfo:&#8217; method.</em></strong></span></p>
<pre class="brush: objc; title: ; notranslate">
- (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(@&quot;Image Path:%@&quot;, [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];
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://cozydev.com/?feed=rss2&amp;p=83</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

