<?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>Orange Juice Liberation Front &#187; objective c</title>
	<atom:link href="http://orangejuiceliberationfront.com/tag/objective-c-9/feed/" rel="self" type="application/rss+xml" />
	<link>http://orangejuiceliberationfront.com</link>
	<description>Uli&#039;s citrussy fresh thoughts on technical stuff</description>
	<lastBuildDate>Mon, 13 May 2013 20:59:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Drawing off-screen in Cocoa</title>
		<link>http://orangejuiceliberationfront.com/drawing-off-screen-in-cocoa/</link>
		<comments>http://orangejuiceliberationfront.com/drawing-off-screen-in-cocoa/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 11:20:07 +0000</pubDate>
		<dc:creator>uliwitness</dc:creator>
				<category><![CDATA[Macintosh]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[drawing]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[macintosh]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blog.orangejuiceliberationfront.com/?p=100</guid>
		<description><![CDATA[In general, drawing in Cocoa always happens in an NSView&#8216;s -drawRect: method on the main thread. The OS calls that whenever you&#8217;re supposed to draw anything, and if you want to redraw, you use -setNeedsDisplay: to ask the OS to call you back, and it will in turn call -drawRect:. But sometimes, you just want &#8230;  <a href="http://orangejuiceliberationfront.com/drawing-off-screen-in-cocoa/">Continue reading</a>]]></description>
				<content:encoded><![CDATA[<p>In general, drawing in Cocoa <em>always</em> happens in an <tt>NSView</tt>&#8216;s <tt>-drawRect:</tt> method on the main thread. The OS calls that whenever you&#8217;re supposed to draw anything, and if you want to redraw, you use <tt>-setNeedsDisplay:</tt> to ask the OS to call you back, and it will in turn call <tt>-drawRect:</tt>.</p>
<p>But sometimes, you just want to draw on a secondary thread, or you want to draw something for later use that shouldn&#8217;t show up in the view right now. The approach mentioned above seems awfully limited, doesn&#8217;t it? Of course there are other ways to do that.</p>
<p>A very easy way is to just take an <tt>NSImage</tt>, create it in your helper thread or threaded <tt>NSOperation</tt>, call <tt>-lockFocus</tt> on it, draw your stuff, call <tt>-unlockFocus</tt> and then send the finished image back off to your main thread using <tt>-performSelectorOnMainThread:withObject:waitUntilDone:</tt>. Note though, that doing a <tt>-lockFocus</tt> on an <tt>NSImage</tt> pretty much deletes and recreates the image contents, along with a Graphics Context needed to draw into the image, so if you plan to lockFocus/unlockFocus a lot, this will be fairly slow. In this case, you might be better off creating an <tt>NSBitmapImageRep</tt> directly and drawing into that, or even creating a <tt>CGBitmapGraphicsContext</tt>, doing your drawing in there, and only creating an <tt>NSImage</tt> from that once you&#8217;re finished.</p>
<p>Since <em>you</em> decide what to do with the <tt>NSImage</tt> once you&#8217;ve created it, this is a decent way to cache drawing that will need to be displayed later, or to pre-render more detailed versions of tiles of an image in the background to allow for more detail when zooming. It is also a way to speed up drawing of complex stuff. The CPU and GPU are fast enough to blast a full-screen pixel buffer to the screen at 50fps or more, but actually doing anti-aliased rendering of bezier paths or laying out text at this rate might be taxing the machine a bit. It also means all your drawing resources are together on your secondary thread, completely disconnected from the main thread. So it&#8217;s unlikely your thread has to wait for someone on the main thread to finish drawing, unnecessarily slowing things down.</p>
<p>If you really, urgently need to draw <em>now</em>, and need to do it on another thread (e.g. for video playback), you&#8217;ll need to do the threaded NSView drawing thing: Call <tt>-lockFocusIfCanDraw</tt> on the view, do your drawing, being careful that all your state has thread-safety locks or is immutable, call <tt>-flushGraphics</tt> on the current context and then <tt>-unlockFocus</tt> again. This is a bit dangerous due to all the thread synchronization points, though, and doesn&#8217;t work all the way back across system releases, so don&#8217;t do that if you can avoid it. If you want to learn more, see <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html" target="_blank">Apple&#8217;s thread-safety documentation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://orangejuiceliberationfront.com/drawing-off-screen-in-cocoa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

 Served from: orangejuiceliberationfront.com @ 2013-05-20 04:57:44 by W3 Total Cache -->