<?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>bunnyhero dev &#187; iPhone</title>
	<atom:link href="http://www.bunnyhero.org/tag/iphone/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bunnyhero.org</link>
	<description>Notes on iPhone, Flash and Web development</description>
	<lastBuildDate>Mon, 07 Nov 2011 21:49:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Loading an image mask from a file</title>
		<link>http://www.bunnyhero.org/2010/07/23/loading-an-image-mask-from-a-file/</link>
		<comments>http://www.bunnyhero.org/2010/07/23/loading-an-image-mask-from-a-file/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 15:00:22 +0000</pubDate>
		<dc:creator>bunnyhero</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[cgimage]]></category>
		<category><![CDATA[coregraphics]]></category>
		<category><![CDATA[graphics]]></category>

		<guid isPermaLink="false">http://www.bunnyhero.org/?p=334</guid>
		<description><![CDATA[Core Graphics image masks are handy, but if you want to load the mask image from a file, things don&#8217;t always work the way you expect. The function CGImageCreateWithMask() can take either a mask or an image as the second parameter, but it turns out that Core Graphics (at least on iOS) is pretty picky [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.bunnyhero.org/wp-content/uploads/2010/07/imagemask-illo.png" alt="illustration of an image being masked" title="image masking" width="450" height="180" class="aligncenter size-full wp-image-347" /><br />
Core Graphics image masks are handy, but if you want to load the mask image from a file, things don&#8217;t always work the way you expect.</p>
<p>The function <a href="http://developer.apple.com/mac/library/documentation/GraphicsImaging/Reference/CGImage/Reference/reference.html#jumpTo_8"><code>CGImageCreateWithMask()</code></a> can take either a mask or an image as the second parameter, but it turns out that Core Graphics (at least on iOS) is pretty picky about what is an acceptable image for the mask.</p>
<p>I&#8217;ve seen this snippet of code suggested in a few places:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CGImageRef mask <span style="color: #002200;">=</span> CGImageMaskCreate<span style="color: #002200;">&#40;</span>CGImageGetWidth<span style="color: #002200;">&#40;</span>image<span style="color: #002200;">&#41;</span>,
CGImageGetHeight<span style="color: #002200;">&#40;</span>image<span style="color: #002200;">&#41;</span>, CGImageGetBitsPerComponent<span style="color: #002200;">&#40;</span>image<span style="color: #002200;">&#41;</span>,
CGImageGetBitsPerPixel<span style="color: #002200;">&#40;</span>image<span style="color: #002200;">&#41;</span>, CGImageGetBytesPerRow<span style="color: #002200;">&#40;</span>image<span style="color: #002200;">&#41;</span>,
CGImageGetDataProvider<span style="color: #002200;">&#40;</span>image<span style="color: #002200;">&#41;</span>, <span style="color: #a61390;">NULL</span>, <span style="color: #a61390;">false</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>the idea being that you create a mask with the pixels that are in the loaded image, but it turns out that this code is not 100% reliable either.</p>
<p>The truth of the matter is that CGImage is an incredibly versatile object. The bits that represent the image can be in a variety of formats, bit depths, and colour space. When you load an image from a file, you are not guaranteed what format those bits are going to be in—for example, there are reports online of how people can get image masks to work if they save it in one way from an image editing program, but not if they save it a different way (e.g. <a href="http://stackoverflow.com/questions/1133248/any-idea-why-this-image-masking-code-does-not-work">http://stackoverflow.com/questions/1133248/any-idea-why-this-image-masking-code-does-not-work</a> )</p>
<p>Thus, I&#8217;ve found that the best and most reliable way to generate an image mask from an arbitrary image is to do this:</p>
<ol>
<li>Create a bitmap graphics context that is in an acceptable format for image masks</li>
<li>Draw your image into this bitmap graphics context</li>
<li>Create the image mask from the bits of the bitmap graphics context.</li>
</ol>
<p>The following function has worked well for me so far:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CGImageRef createMaskWithImage<span style="color: #002200;">&#40;</span>CGImageRef image<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">int</span> maskWidth               <span style="color: #002200;">=</span> CGImageGetWidth<span style="color: #002200;">&#40;</span>image<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">int</span> maskHeight              <span style="color: #002200;">=</span> CGImageGetHeight<span style="color: #002200;">&#40;</span>image<span style="color: #002200;">&#41;</span>;
    <span style="color: #11740a; font-style: italic;">//  round bytesPerRow to the nearest 16 bytes, for performance's sake</span>
    <span style="color: #a61390;">int</span> bytesPerRow             <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>maskWidth <span style="color: #002200;">+</span> <span style="color: #2400d9;">15</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span> 0xfffffff0;
    <span style="color: #a61390;">int</span> bufferSize              <span style="color: #002200;">=</span> bytesPerRow <span style="color: #002200;">*</span> maskHeight;
&nbsp;
    <span style="color: #11740a; font-style: italic;">//  we use CFData instead of malloc(), because the memory has to stick around</span>
    <span style="color: #11740a; font-style: italic;">//  for the lifetime of the mask. if we used malloc(), we'd have to</span>
    <span style="color: #11740a; font-style: italic;">//  tell the CGDataProvider how to dispose of the memory when done. using</span>
    <span style="color: #11740a; font-style: italic;">//  CFData is just easier and cleaner.</span>
&nbsp;
    CFMutableDataRef dataBuffer <span style="color: #002200;">=</span> CFDataCreateMutable<span style="color: #002200;">&#40;</span>kCFAllocatorDefault, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
    CFDataSetLength<span style="color: #002200;">&#40;</span>dataBuffer, bufferSize<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">//  the data will be 8 bits per pixel, no alpha</span>
    CGColorSpaceRef colourSpace <span style="color: #002200;">=</span> CGColorSpaceCreateDeviceGray<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
    CGContextRef ctx            <span style="color: #002200;">=</span> CGBitmapContextCreate<span style="color: #002200;">&#40;</span>CFDataGetMutableBytePtr<span style="color: #002200;">&#40;</span>dataBuffer<span style="color: #002200;">&#41;</span>,
                                                        maskWidth, maskHeight,
                                                        <span style="color: #2400d9;">8</span>, bytesPerRow, colourSpace, kCGImageAlphaNone<span style="color: #002200;">&#41;</span>;
    <span style="color: #11740a; font-style: italic;">//  drawing into this context will draw into the dataBuffer.</span>
    CGContextDrawImage<span style="color: #002200;">&#40;</span>ctx, CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, maskWidth, maskHeight<span style="color: #002200;">&#41;</span>, image<span style="color: #002200;">&#41;</span>;
    CGContextRelease<span style="color: #002200;">&#40;</span>ctx<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">//  now make a mask from the data.</span>
    CGDataProviderRef dataProvider  <span style="color: #002200;">=</span> CGDataProviderCreateWithCFData<span style="color: #002200;">&#40;</span>dataBuffer<span style="color: #002200;">&#41;</span>;
    CGImageRef mask                 <span style="color: #002200;">=</span> CGImageMaskCreate<span style="color: #002200;">&#40;</span>maskWidth, maskHeight, <span style="color: #2400d9;">8</span>, <span style="color: #2400d9;">8</span>, bytesPerRow,
                                                        dataProvider, <span style="color: #a61390;">NULL</span>, FALSE<span style="color: #002200;">&#41;</span>;
&nbsp;
    CGDataProviderRelease<span style="color: #002200;">&#40;</span>dataProvider<span style="color: #002200;">&#41;</span>;
    CGColorSpaceRelease<span style="color: #002200;">&#40;</span>colourSpace<span style="color: #002200;">&#41;</span>;
    CFRelease<span style="color: #002200;">&#40;</span>dataBuffer<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> mask;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Example of use:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">UIImage <span style="color: #002200;">*</span>maskSource <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mask.png&quot;</span><span style="color: #002200;">&#93;</span>;
CGImageRef mask <span style="color: #002200;">=</span> createMaskWithImage<span style="color: #002200;">&#40;</span>maskSource.CGImage<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Then use the mask as you wish, for example in the aforementioned <code>CGImageCreateWithMask()</code> or <a href="http://developer.apple.com/mac/library/documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html#//apple_ref/doc/uid/TP30000950-CH1g-BCIICIIE"><code>CGContextClipToMask()</code></a></p>
<p>And don&#8217;t forget to dispose of the mask when you&#8217;re done. <code>createMaskWithImage()</code> returns the mask with a retain count of 1, and expects the caller to take ownership.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CGImageRelease<span style="color: #002200;">&#40;</span>mask<span style="color: #002200;">&#41;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.bunnyhero.org/2010/07/23/loading-an-image-mask-from-a-file/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>iPhone VR: Viewing 3D panoramas in Safari using JavaScript and -webkit-transform</title>
		<link>http://www.bunnyhero.org/2008/10/13/iphone-vr-viewing-3d-panoramas-in-safari-using-javascript-and-webkit-transform/</link>
		<comments>http://www.bunnyhero.org/2008/10/13/iphone-vr-viewing-3d-panoramas-in-safari-using-javascript-and-webkit-transform/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 03:59:48 +0000</pubDate>
		<dc:creator>bunnyhero</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[ipod touch]]></category>
		<category><![CDATA[safari]]></category>

		<guid isPermaLink="false">http://www.bunnyhero.org/?p=81</guid>
		<description><![CDATA[Apple&#8217;s 2.0 iPhone update brought some interesting enhancements to Mobile Safari, including 3-D perspective and access to multitouch events. Armed with the documentation (of varying quality) from Apple&#8217;s Web Apps Dev Centre, and invaluable information from blog articles such as Idean&#8217;s &#8220;Spin the Bottle&#8221;, SitePen&#8217;s &#8220;Touching and Gesturing on the iPhone&#8221; and Paul Bakaus&#8217;s &#8220;3D [...]]]></description>
			<content:encoded><![CDATA[<p>Apple&#8217;s 2.0 iPhone update brought some interesting enhancements to Mobile Safari, including 3-D perspective and access to multitouch events. Armed with the documentation (of varying quality) from <a href="http://developer.apple.com/webapps/">Apple&#8217;s Web Apps Dev Centre</a>, and invaluable information from blog articles such as <a href="http://www.idean.com/blog/spin-the-bottle/">Idean&#8217;s &#8220;Spin the Bottle&#8221;</a>, <a href="http://www.sitepen.com/blog/2008/07/10/touching-and-gesturing-on-the-iphone/">SitePen&#8217;s &#8220;Touching and Gesturing on the iPhone&#8221;</a> and <a href="http://paulbakaus.com/?p=15&#038;language=en">Paul Bakaus&#8217;s &#8220;3D CSS Transforms on the iPhone&#8221;</a>, I put together a <a href="http://tinyurl.com/iphonevr">simple web app</a> that displays a <a href="http://www.panoguide.com/howto/panoramas/types.jsp">cubic panorama</a> in Mobile Safari. Look left, right, up and down by dragging your finger across the display. No zoom, currently, though. It was pretty straightforward, but I did learn a few things while creating it.</p>
<p>Go ahead, take a look :D Visit <a href="http://tinyurl.com/iphonevr">http://tinyurl.com/iphonevr</a> on your iPhone or iPod Touch.</p>
<p><img src="http://www.bunnyhero.org/wp-content/uploads/2008/10/photo-1-200x300.jpg" alt="" title="iphone vr 1" width="200" height="300" class="alignnone size-medium wp-image-84" /> <img src="http://www.bunnyhero.org/wp-content/uploads/2008/10/photo-200x300.jpg" alt="" title="iphone vr 2" width="200" height="300" class="alignnone size-medium wp-image-86" /></p>
<p>The images used for the panorama are taken from one of the QuickTime VR movies in <a href="http://www.apple.com/quicktime/gallery/cubicvr/">Apple&#8217;s Cubic VR gallery</a>.  I extracted the images from the movie using <a href="http://www.qtbridge.com/LS/Tips/GetImagesFromVR/index.html">the procedure explained here</a> (in French, but it&#8217;s easy to follow).</p>
<p>I may write a full tutorial on this eventually, but for now I will just share a few notes (and the <a href="http://www.bunnyhero.org/static/iphonevr/iphone-vr.zip">source code</a>):</p>
<ul>
<li>It&#8217;s a lot easier to get the 3-d transforms right if the elements you want to transform start out centred in their containing block (so that they start at (0, 0, 0), essentially). I did this with regular CSS, but I suppose this could also be done as part of the transform.
</li>
<li>The above also applies (especially) to any DIV that contains nothing but other DIVs. At first my cube was spinning around its top edge, instead of its centre, because its children (the cube faces) were absolute-positioned, and thus the &#8220;cube&#8221; DIV itself had no height! Giving it an explicit width and height solved this.
</li>
<li>Touch events on 3-D transformed elements seemed to be little bit unreliable. I worked around this by putting a normal transparent DIV on top of the whole thing and listened to events on that instead.
</li>
</ul>
<p><a href="http://www.bunnyhero.org/static/iphonevr/iphone-vr.zip">Download the source code</a> (MIT Licence).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bunnyhero.org/2008/10/13/iphone-vr-viewing-3d-panoramas-in-safari-using-javascript-and-webkit-transform/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

