<?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>iPhoneGeek 爱疯极客 &#187; 相片簿</title>
	<atom:link href="http://www.iphone-geek.cn/tag/%e7%9b%b8%e7%89%87%e7%b0%bf/feed" rel="self" type="application/rss+xml" />
	<link>http://www.iphone-geek.cn</link>
	<description>iPhone 新闻，编程，技巧与提示，代码，教程</description>
	<lastBuildDate>Thu, 08 Dec 2011 01:18:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>存储OpenGL ES内容到相片簿中</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%ad%98%e5%82%a8opengl-es%e5%86%85%e5%ae%b9%e5%88%b0%e7%9b%b8%e7%89%87%e7%b0%bf%e4%b8%ad</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%ad%98%e5%82%a8opengl-es%e5%86%85%e5%ae%b9%e5%88%b0%e7%9b%b8%e7%89%87%e7%b0%bf%e4%b8%ad#comments</comments>
		<pubDate>Wed, 30 Dec 2009 02:40:57 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[图形图像]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[相片簿]]></category>
		<category><![CDATA[OpenGL ES]]></category>
		<category><![CDATA[代码片段]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=578</guid>
		<description><![CDATA[（注：本文改编自iPhone – saving OpenGL ES content to the Photo Album，懒得全文翻译了，只挑一下重点加上自己的语言描述一下）
&#160;
有许多程序都有将图片存入相片簿的功能。如果你使用了OpenGL ES，那么本文的方法适合你使用。简单步骤：

使用glReadPixels读取GL数据到字节数组中
使用UIImageWriteToSavedPhotosAlbum将UIImage存储到相片簿中

&#160;
但怎样将字节数组转换成UIImage比较困难。由于 [UIImage imageFromData:data]需要UIImage支持的文件格式，所以并不适用，因为从glReadPixels读取的字节数组中的数据是原始像素数据。可以使用CGImageCreate来创建一个CGImageRef，然后用[UIImage imageWithCGImage:imageRef]转换成UIImage。CGImageCreate要求CGDataProviderRef，可以通过CGDataProviderCreateWithData使用glReadPixels的数据来创建。整个过程的流程如下：
&#160;
glReadPixels -&#62; CGDataProviderCreateWithData -&#62; CGImageCreate -&#62; [UIImage imageWithCGImage:] -&#62; UIImageWriteToSavedPhotosAlbum
&#160;
但是还存在一个问题，OpenGL 使用标准笛卡尔坐标，即+Y朝上， -Y朝下。所以用glReadPixels获得的数组是上下颠倒的。可以使用位旋转技术进行修正。下面是代码（用在具有CAEAGLLayer的UIView中） ：
123456789101112131415161718192021222324252627282930313233343536373839404142434445&#160; &#160; &#160; -&#40;UIImage *&#41; glToUIImage &#123;
&#160; &#160;
&#160; &#160; &#160; &#160; &#160; NSInteger myDataLength = 320 * 480 * 4;
&#160; &#160; &#160; &#160; &#160; &#160;
&#160; &#160; &#160; &#160; &#160; // [...]]]></description>
			<content:encoded><![CDATA[<p>（注：本文改编自<a title="Permanent Link: iPhone – saving OpenGL ES content to the Photo Album" rel="bookmark" href="http://www.bit-101.com/blog/?p=1861">iPhone – saving OpenGL ES content to the Photo Album</a>，懒得全文翻译了，只挑一下重点加上自己的语言描述一下）</p>
<p>&nbsp;</p>
<p>有许多程序都有将图片存入相片簿的功能。如果你使用了OpenGL ES，那么本文的方法适合你使用。简单步骤：</p>
<ol>
<li>使用glReadPixels读取GL数据到字节数组中</li>
<li>使用UIImageWriteToSavedPhotosAlbum将UIImage存储到相片簿中</li>
</ol>
<p>&nbsp;</p>
<p>但怎样将字节数组转换成UIImage比较困难。由于 [UIImage imageFromData:data]需要UIImage支持的文件格式，所以并不适用，因为从glReadPixels读取的字节数组中的数据是原始像素数据。可以使用CGImageCreate来创建一个CGImageRef，然后用[UIImage imageWithCGImage:imageRef]转换成UIImage。CGImageCreate要求CGDataProviderRef，可以通过CGDataProviderCreateWithData使用glReadPixels的数据来创建。整个过程的流程如下：</p>
<p>&nbsp;</p>
<p>glReadPixels -&gt; CGDataProviderCreateWithData -&gt; CGImageCreate -&gt; [UIImage imageWithCGImage:] -&gt; UIImageWriteToSavedPhotosAlbum</p>
<p>&nbsp;</p>
<p>但是还存在一个问题，OpenGL 使用标准笛卡尔坐标，即+Y朝上， -Y朝下。所以用glReadPixels获得的数组是上下颠倒的。可以使用位旋转技术进行修正。下面是代码（用在具有CAEAGLLayer的UIView中） ：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; <span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>UIImage <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> glToUIImage <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NSInteger myDataLength <span style="color: #002200;">=</span> 320 <span style="color: #002200;">*</span> 480 <span style="color: #002200;">*</span> <span style="color: #2400d9;">4</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// allocate array and read pixels into it.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GLubyte <span style="color: #002200;">*</span>buffer <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/malloc.html"><span style="color: #a61390;">malloc</span></a><span style="color: #002200;">&#40;</span>myDataLength<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glReadPixels<span style="color: #002200;">&#40;</span>0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #11740a; font-style: italic;">// gl renders &quot;upside down&quot; so swap top to bottom into new array.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #11740a; font-style: italic;">// there's gotta be a better way, but this works.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GLubyte <span style="color: #002200;">*</span>buffer2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>GLubyte <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/malloc.html"><span style="color: #a61390;">malloc</span></a><span style="color: #002200;">&#40;</span>myDataLength<span style="color: #002200;">&#41;</span>;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> y <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; y &lt;<span style="color: #2400d9;">480</span>; y<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> x <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; x &lt;320 <span style="color: #002200;">*</span> <span style="color: #2400d9;">4</span>; x<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer2<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>479 <span style="color: #002200;">-</span> y<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> 320 <span style="color: #002200;">*</span> 4 <span style="color: #002200;">+</span> x<span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> buffer<span style="color: #002200;">&#91;</span>y <span style="color: #002200;">*</span> 4 <span style="color: #002200;">*</span> 320 <span style="color: #002200;">+</span> x<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// make data provider with data.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CGDataProviderRef provider <span style="color: #002200;">=</span> CGDataProviderCreateWithData<span style="color: #002200;">&#40;</span><span style="color: #a61390;">NULL</span>, buffer2, myDataLength, <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// prep the ingredients</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">int</span> bitsPerComponent <span style="color: #002200;">=</span> <span style="color: #2400d9;">8</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">int</span> bitsPerPixel <span style="color: #002200;">=</span> <span style="color: #2400d9;">32</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">int</span> bytesPerRow <span style="color: #002200;">=</span> 4 <span style="color: #002200;">*</span> <span style="color: #2400d9;">320</span>;<br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; CGColorSpaceRef colorSpaceRef <span style="color: #002200;">=</span> CGColorSpaceCreateDeviceRGB<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; CGBitmapInfo bitmapInfo <span style="color: #002200;">=</span> kCGBitmapByteOrderDefault;<br />
&nbsp; &nbsp; &nbsp; &nbsp; CGColorRenderingIntent renderingIntent <span style="color: #002200;">=</span> kCGRenderingIntentDefault;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// make the cgimage</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CGImageRef imageRef <span style="color: #002200;">=</span> CGImageCreate<span style="color: #002200;">&#40;</span>320, 480, bitsPerComponent, bitsPerPixel, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bytesPerRow, colorSpaceRef, bitmapInfo, provider, <span style="color: #a61390;">NULL</span>, <span style="color: #a61390;">NO</span>, renderingIntent<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// then make the uiimage from that</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; UIImage <span style="color: #002200;">*</span>myImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageWithCGImage<span style="color: #002200;">:</span>imageRef<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">return</span> myImage;<br />
&nbsp; &nbsp;<span style="color: #002200;">&#125;</span><br />
&nbsp;<br />
&nbsp; &nbsp;<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>captureToPhotoAlbum <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self glToUIImage<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UIImageWriteToSavedPhotosAlbum<span style="color: #002200;">&#40;</span>image, self, <span style="color: #a61390;">nil</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>; <br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%ad%98%e5%82%a8opengl-es%e5%86%85%e5%ae%b9%e5%88%b0%e7%9b%b8%e7%89%87%e7%b0%bf%e4%b8%ad/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

