<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	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/"
	>

<channel>
	<title>farp.blog &#187; coregraphics</title>
	<atom:link href="http://fredandrandall.com/blog/tag/coregraphics/feed/" rel="self" type="application/rss+xml" />
	<link>http://fredandrandall.com/blog</link>
	<description>Bloggin&#039; about whatever</description>
	<lastBuildDate>Tue, 04 Nov 2014 07:15:30 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>Afternoon Apps: Captionator</title>
		<link>http://fredandrandall.com/blog/2012/08/20/afternoon-apps-captionator/</link>
		<comments>http://fredandrandall.com/blog/2012/08/20/afternoon-apps-captionator/#comments</comments>
		<pubDate>Mon, 20 Aug 2012 04:47:19 +0000</pubDate>
		<dc:creator><![CDATA[Randall]]></dc:creator>
				<category><![CDATA[Afternoon Apps]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[coregraphics]]></category>
		<category><![CDATA[iPhone SDK]]></category>
		<category><![CDATA[quartzcore]]></category>

		<guid isPermaLink="false">http://fredandrandall.com/blog/?p=752</guid>
		<description><![CDATA[A couple weeks ago, a friend of mine showed me the app I&#8217;d Cap That. After making some hilarious pictures, and after a few drinks, I made the claim that I could probably clone the app in a couple of &#8230; <a href="http://fredandrandall.com/blog/2012/08/20/afternoon-apps-captionator/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><a href="http://fredandrandall.com/blog/wp-content/uploads/2012/08/photo-3.jpg"><img class="alignleft size-medium wp-image-756" title="Captionator" src="http://fredandrandall.com/blog/wp-content/uploads/2012/08/photo-3-300x225.jpg" alt="" width="300" height="225" /></a>A couple weeks ago, a friend of mine showed me the app <a href="http://itunes.apple.com/us/app/id-cap-that/id510544616?mt=8">I&#8217;d Cap That</a>. After making some hilarious pictures, and after a few drinks, I made the claim that I could probably clone the app in a couple of hours.</p>
<p>So the other day, that&#8217;s what I tried to do. The app is very simple. It takes a randomly generated caption and burns it on to an image. You can pick an image from your camera or your library.</p>
<p>When I wrote the BD&#8217;s Mongolian BBQ app, I took stamps and burned them onto an image to create funny mustache pictures. The way I did that was to position UIImageViews inside of a UIView and then render the view to an image. That strategy works fine, but it also drops the image resolution down quite a bit. (It will be whatever the size of the view is)</p>
<p>So this time, I decided to render the text directly onto the image by hand. This involves getting a graphics context, drawing the image into it, then drawing the text into it, and finally getting the resulting UIImage from the graphics context.</p>
<p>My first approach was to use a UILabel to render the text onto the image. That would take care of all the font sizing issues and save some code. This worked great, until I had white text on a light image. There&#8217;s a couple ways to solve this problem like putting a stroke around the text or adding a shadow to the text. I&#8217;d Cap That uses a shadow. I decided to go for a more meme style look and stroke the text.</p>
<p>Unfortunately, UILabel doesn&#8217;t support adding a stroke around the text. So I looked for some classes online that might get the job done. I found a couple, but they didn&#8217;t work like I wanted. Luckily, CoreGraphics lets you draw stroked text, you just have to handle more by yourself.</p>
<pre class="brush: objc; title: ; notranslate">
-(UIImage*)getImage:(UIImage*)image withCaption:(NSString*)captionString
{
   UIGraphicsBeginImageContext([image size]);
   [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];

   CGFloat captionFontSize = 300;
   CGSize captionSize = [captionString sizeWithFont:[UIFont boldSystemFontOfSize:captionFontSize]];

   while( captionSize.width &gt; image.size.width )
   {
      captionFontSize -= 5;
      captionSize = [captionString sizeWithFont:[UIFont boldSystemFontOfSize:captionFontSize]];
   }

   CGRect captionRect = CGRectMake((image.size.width - captionSize.width)/2.0, (image.size.height * .95)-captionSize.height, captionSize.width, captionSize.height);

   CGContextRef context = UIGraphicsGetCurrentContext();
   CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
   CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
   CGContextSetLineWidth(context, 3.0f);
   CGContextSetTextDrawingMode(context, kCGTextFillStroke);

   [captionString drawInRect:captionRect withFont:[UIFont boldSystemFontOfSize:captionFontSize]];

   UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();

   UIGraphicsEndImageContext();
   return outputImage;
}
</pre>
<p>The problem with rendering the text yourself is that you need to size the font yourself. The way I&#8217;m doing it is picking a large font size (300 in this case) then measuring it and adjusting the font size until it fits on the image. There&#8217;s likely a better way, but this works and is fast enough.</p>
<p>I haven&#8217;t decided if I&#8217;m gonna release this app or not, like I did with my other <a title="Afternoon Apps: Descrumbled" href="http://fredandrandall.com/blog/2011/11/06/afternoon-apps-descrumbled/">Afternoon</a> <a title="Afternoon Apps 2: Scrumbled" href="http://fredandrandall.com/blog/2011/11/21/afternoon-apps-2-scrumbled/">Apps</a>. I have the <a href="https://github.com/blladnar/Captionator">source code up on GitHub</a> though, so feel free to take a look and use whatever you want from it.</p>
<p>There&#8217;s some issues with it. The camera view doesn&#8217;t work quite right. There aren&#8217;t any icons. One little feature in there is you can add to the list of captions. Of course the real secret sauce of I&#8217;d Cap That isn&#8217;t the app, it&#8217;s the hilarious captions.</p>
]]></content:encoded>
			<wfw:commentRss>http://fredandrandall.com/blog/2012/08/20/afternoon-apps-captionator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
