<?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; Core Graphics</title>
	<atom:link href="http://www.iphone-geek.cn/tag/core-graphics/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>pdf渲染的小窍门</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/pdf%e6%b8%b2%e6%9f%93%e7%9a%84%e5%b0%8f%e7%aa%8d%e9%97%a8</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/pdf%e6%b8%b2%e6%9f%93%e7%9a%84%e5%b0%8f%e7%aa%8d%e9%97%a8#comments</comments>
		<pubDate>Thu, 21 Apr 2011 04:10:45 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[图形图像]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[Core Graphics]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[代码片段]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=1164</guid>
		<description><![CDATA[ ]]></description>
			<content:encoded><![CDATA[<p>我们都知道，在iPhone/iPad显示pdf的基本方法有两个，一个是使用UIWebView直接加载pdf文件，另一个是使用Core Graphics进行渲染（姑且称之为CGPDF方法）。UIWebView的方法是简单，只需加载pdf，其他诸如放大翻页等问题通通交给UIWebView去头痛吧。但其缺点是性能较慢，功能有限，比如要实现搜索，添加笔记等功能就比较难。而使用CGPDF方法，功能就没有限制（虽然pdf解析方面，苹果提供的文档实在有限），使用Core Graphics进行渲染，性能上也比UIWebView要提高许多，只不过翻页，放大缩小等功能都需要自己实现。<br class="spacer_" /><br />
<span id="more-1164"></span><br />
有关pdf放大缩小，翻页等功能可以使用UIScrollView实现，不在本文讨论的范围之内。笔者在项目中使用了CGPDF的过程中，曾遇到两个小问题，因此，在这里总结一下：<br />
<br class="spacer_" /></p>
<h2>1. 页面放大后变得不清晰<br />
<br class="spacer_" /></h2>
<p>先看看我程序中的渲染代码，可以放在- (void)drawRect:(CGRect)rect 或者 -(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context 中：<br class="spacer_" /></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 /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>rect<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; CGContextRef context <span style="color: #002200;">=</span> UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// fill in the white background for pdf page</span><br />
&nbsp; &nbsp; CGContextSetRGBFillColor<span style="color: #002200;">&#40;</span> context, 1.0, 1.0, 1.0, 1.0 <span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; CGContextFillRect<span style="color: #002200;">&#40;</span> context, CGContextGetClipBoundingBox<span style="color: #002200;">&#40;</span> context <span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; CGContextSaveGState<span style="color: #002200;">&#40;</span>context<span style="color: #002200;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Flip the context so that the PDF page is rendered</span><br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// right side up.</span><br />
&nbsp; &nbsp; CGContextTranslateCTM<span style="color: #002200;">&#40;</span> context, 0.0, self.bounds.size.height <span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; CGContextScaleCTM<span style="color: #002200;">&#40;</span> context, 1.0, <span style="color: #002200;">-</span>1.0 <span style="color: #002200;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// Scale the context so that the PDF page is rendered</span><br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// at the correct size for the zoom level.</span><br />
&nbsp; &nbsp; CGAffineTransform pdfXfm <span style="color: #002200;">=</span><br />
&nbsp; &nbsp; CGPDFPageGetDrawingTransform<span style="color: #002200;">&#40;</span> _page.page, kCGPDFMediaBox, self.bounds, 0, <span style="color: #a61390;">true</span> <span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; CGContextConcatCTM<span style="color: #002200;">&#40;</span> context, pdfXfm <span style="color: #002200;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; CGContextSetInterpolationQuality<span style="color: #002200;">&#40;</span>context, kCGInterpolationHigh<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; CGContextSetRenderingIntent<span style="color: #002200;">&#40;</span>context, kCGRenderingIntentDefault<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; CGContextDrawPDFPage<span style="color: #002200;">&#40;</span> context, _page.page <span style="color: #002200;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; CGContextRestoreGState<span style="color: #002200;">&#40;</span>context<span style="color: #002200;">&#41;</span>;<br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
<p><br class="spacer_" /><br />
简单地说，这段代码就是使用Core Graphics进行pdf的渲染，可是我的pdf放大后为什么不清晰呢？先看看放大pdf后可以使其清晰的部分代码吧：<br class="spacer_" /></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 /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithPdfPage<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>PDFPage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>page<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>0, 0, page.size.width, page.size.height<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CATiledLayer<span style="color: #002200;">*</span> layer <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CATiledLayer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>self layer<span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; layer.levelsOfDetail <span style="color: #002200;">=</span> <span style="color: #2400d9;">4</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; layer.levelsOfDetailBias <span style="color: #002200;">=</span> <span style="color: #2400d9;">4</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; layer.tileSize <span style="color: #002200;">=</span> CGSizeMake<span style="color: #002200;">&#40;</span>1024.0, 1024.0<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> self;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">Class</span><span style="color: #002200;">&#41;</span>layerClass<br />
<span style="color: #002200;">&#123;</span><br />
<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>CATiledLayer class<span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
<p><br class="spacer_" /><br />
其秘诀就在CATiledLayer，简单地说就是CATiledLayer将不同等级的tiles（拼贴）缓存起来，而CGContextDrawPDFPage则根据最合适的放大等级将pdf渲染出来。<br class="spacer_" /></p>
<p>根据文档CATiledLayer可以更高效，高质量地渲染pdf文档，但我则遇到了第二个问题。<br />
<br class="spacer_" /></p>
<h2>2. 页面渲染速度慢，而且呈块状渲染<br />
<br class="spacer_" /></h2>
<p>简而言之就是pdf页面一块一块地慢慢渲染出来，效果非常不好。这是怎么回事？经过一番研究发现是CATiledLayer的动画效果在作怪。每个Tile的渲染据说都有0.25s的动画时间，其结果就是pdf文档一块一块地出现了。要怎样解决这个问题呢？最为直接的方法就是把0.25s的动画时间直接设置为0。下面是代码（采用继承CATiledLayer的方法）：<br class="spacer_" /></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 /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">@interface</span> FastCATiledLayer <span style="color: #002200;">:</span> CATiledLayer<br />
<span style="color: #a61390;">@end</span><br />
<br />
<span style="color: #a61390;">@implementation</span> FastCATiledLayer<br />
<span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span>CFTimeInterval<span style="color: #002200;">&#41;</span>fadeDuration <br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0.0</span>;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">Class</span><span style="color: #002200;">&#41;</span>layerClass <br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>FastCATiledLayer class<span style="color: #002200;">&#93;</span>; &nbsp; <br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithPdfPage<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>PDFPage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>page <br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>0, 0, page.size.width, page.size.height<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; FastCATiledLayer<span style="color: #002200;">*</span> layer <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>FastCATiledLayer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>self layer<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; layer.levelsOfDetail <span style="color: #002200;">=</span> <span style="color: #2400d9;">4</span>;<br />
&nbsp; &nbsp; layer.levelsOfDetailBias <span style="color: #002200;">=</span> <span style="color: #2400d9;">4</span>;<br />
&nbsp; &nbsp; layer.tileSize <span style="color: #002200;">=</span> CGSizeMake<span style="color: #002200;">&#40;</span>1024.0, 1024.0<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> self;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
<h5>注:PDFPage是我自己的一个类。</h5>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/pdf%e6%b8%b2%e6%9f%93%e7%9a%84%e5%b0%8f%e7%aa%8d%e9%97%a8/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>怎样从core graphics获取UIImage</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e6%80%8e%e6%a0%b7%e4%bb%8ecore-graphics%e8%8e%b7%e5%8f%96uiimage</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e6%80%8e%e6%a0%b7%e4%bb%8ecore-graphics%e8%8e%b7%e5%8f%96uiimage#comments</comments>
		<pubDate>Mon, 06 Jul 2009 07:37:25 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[图形图像]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[Core Graphics]]></category>
		<category><![CDATA[UIImage]]></category>
		<category><![CDATA[代码片段]]></category>

		<guid isPermaLink="false">http://iphonegeek.zuesitech.com/?p=160</guid>
		<description><![CDATA[从core graphics获取UIImage的方法：
&#160;
12345678910111213141516- &#40;void&#41;viewDidLoad 
&#123;
&#160; &#160; &#91;super viewDidLoad&#93;;

&#160; &#160; UIGraphicsBeginImageContext&#40;CGSizeMake&#40;20,20&#41;&#41;;
&#160; &#160; CGContextRef ctx = UIGraphicsGetCurrentContext&#40;&#41;;
&#160; &#160; CGContextBeginPath&#40;ctx&#41;;
&#160; &#160; CGContextAddArc&#40;ctx, 10, 10, 10, 0, 2*M_PI, 1&#41;;
&#160; &#160; CGContextSetRGBFillColor&#40;ctx, 1,0, 0, 1&#41;;
&#160; &#160; CGContextFillPath&#40;ctx&#41;;
&#160; &#160; UIImage *redBall = UIGraphicsGetImageFromCurrentImageContext&#40;&#41;;
&#160; &#160; UIGraphicsEndImageContext&#40;&#41;;
&#160; &#160; UIImageView *redBallView = &#91;&#91;UIImageView alloc&#93; initWithImage:redBall&#93;;
&#160; &#160; redBallView.center = CGPointMake&#40;160,330&#41;;
&#160; &#160; &#91;self.view addSubview:redBallView&#93;;
&#125;
]]></description>
			<content:encoded><![CDATA[<p>从core graphics获取UIImage的方法：</p>
<p>&nbsp;</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 /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; UIGraphicsBeginImageContext<span style="color: #002200;">&#40;</span>CGSizeMake<span style="color: #002200;">&#40;</span>20,20<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; CGContextRef ctx <span style="color: #002200;">=</span> UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; CGContextBeginPath<span style="color: #002200;">&#40;</span>ctx<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; CGContextAddArc<span style="color: #002200;">&#40;</span>ctx, 10, 10, 10, 0, 2<span style="color: #002200;">*</span>M_PI, 1<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; CGContextSetRGBFillColor<span style="color: #002200;">&#40;</span>ctx, 1,0, 0, 1<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; CGContextFillPath<span style="color: #002200;">&#40;</span>ctx<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; UIImage <span style="color: #002200;">*</span>redBall <span style="color: #002200;">=</span> UIGraphicsGetImageFromCurrentImageContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; UIGraphicsEndImageContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; UIImageView <span style="color: #002200;">*</span>redBallView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImageView alloc<span style="color: #002200;">&#93;</span> initWithImage<span style="color: #002200;">:</span>redBall<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; redBallView.center <span style="color: #002200;">=</span> CGPointMake<span style="color: #002200;">&#40;</span>160,330<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>redBallView<span style="color: #002200;">&#93;</span>;<br />
<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/%e6%80%8e%e6%a0%b7%e4%bb%8ecore-graphics%e8%8e%b7%e5%8f%96uiimage/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

