<?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>NSCoriolisBlog &#187; cocoa</title>
	<atom:link href="http://blog.coriolis.ch/tag/cocoa/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.coriolis.ch</link>
	<description>Just another Coriolis weblog</description>
	<lastBuildDate>Mon, 01 Aug 2011 11:36:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Arbitrary rotation of a CGImage</title>
		<link>http://blog.coriolis.ch/2009/09/04/arbitrary-rotation-of-a-cgimage/</link>
		<comments>http://blog.coriolis.ch/2009/09/04/arbitrary-rotation-of-a-cgimage/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 09:13:50 +0000</pubDate>
		<dc:creator>Stephan</dc:creator>
				<category><![CDATA[cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[cgimage]]></category>
		<category><![CDATA[cgimageref]]></category>
		<category><![CDATA[rotation]]></category>

		<guid isPermaLink="false">http://blog.coriolis.ch/?p=206</guid>
		<description><![CDATA[For my current project, I have to rotate a CGImageRef by an arbitrary angle. Here is the code: - (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle { CGFloat angleInRadians = angle * (M_PI / 180); CGFloat width = CGImageGetWidth(imgRef); CGFloat height = CGImageGetHeight(imgRef); CGRect imgRect = CGRectMake(0, 0, width, height); CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians); CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform); CGColorSpaceRef [...]]]></description>
			<content:encoded><![CDATA[<p>For my current project, I have to rotate a CGImageRef by an arbitrary angle.</p>
<p>Here is the code:</p>
<pre><code>
- (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle
{
  CGFloat angleInRadians = angle * (M_PI / 180);
  CGFloat width = CGImageGetWidth(imgRef);
  CGFloat height = CGImageGetHeight(imgRef);

  CGRect imgRect = CGRectMake(0, 0, width, height);
  CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);
  CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);

  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  CGContextRef bmContext = CGBitmapContextCreate(NULL,
                                                 rotatedRect.size.width,
                                                 rotatedRect.size.height,
                                                 8,
                                                 0,
                                                 colorSpace,
                                                 kCGImageAlphaPremultipliedFirst);
  CGContextSetAllowsAntialiasing(bmContext, FALSE);
  CGContextSetInterpolationQuality(bmContext, kCGInterpolationNone);
  CGColorSpaceRelease(colorSpace);
  CGContextTranslateCTM(bmContext,
                        +(rotatedRect.size.width/2),
                        +(rotatedRect.size.height/2));
  CGContextRotateCTM(bmContext, angleInRadians);
  CGContextTranslateCTM(bmContext,
                        -(rotatedRect.size.width/2),
                        -(rotatedRect.size.height/2));
  CGContextDrawImage(bmContext, CGRectMake(0, 0,
                                           rotatedRect.size.width,
                                           rotatedRect.size.height),
                     imgRef);

  CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext);
  CFRelease(bmContext);
  [(id)rotatedImage autorelease];

  return rotatedImage;
}
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.coriolis.ch/2009/09/04/arbitrary-rotation-of-a-cgimage/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Macros for Xcode</title>
		<link>http://blog.coriolis.ch/2009/01/05/macros-for-xcode/</link>
		<comments>http://blog.coriolis.ch/2009/01/05/macros-for-xcode/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 10:02:56 +0000</pubDate>
		<dc:creator>Stephan</dc:creator>
				<category><![CDATA[cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[nslog]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://blog.coriolis.ch/?p=65</guid>
		<description><![CDATA[These are some of the macros I use with Xcode: CMLog: I use this macro to replace NSLog: #define CMLog(format, ...) NSLog(@"%s:%@", __PRETTY_FUNCTION__,[NSString stringWithFormat:format, ## __VA_ARGS__]); When you use this macro, it outputs text to the console, including the class and method from where it was called. So, if you call this macro from the [...]]]></description>
			<content:encoded><![CDATA[<p>These are some of the macros I use with Xcode:</p>
<p><strong>CMLog</strong>: I use this macro to replace NSLog:</p>
<pre><code>#define CMLog(format, ...) NSLog(@"%s:%@", __PRETTY_FUNCTION__,[NSString stringWithFormat:format, ## __VA_ARGS__]);
</code></pre>
<p>When you use this macro, it outputs text to the console, including the class and method from where it was called. So, if you call this macro from the class <strong>MyAppDelegate</strong> and the method <strong>applicationDidFinishLaunching</strong>,</p>
<pre><code>CMLog(@"My iPhone is an %@, v %@", [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion]);</code></pre>
<p>you get this in the console:<br />
<span id="more-65"></span></p>
<pre><code>2009-01-05 10:06:28.957 MyApp15173:20b] -[MyAppDelegate applicationDidFinishLaunching:]:My iPhone is an iPhone Simulator, v 2.2</code></pre>
<p><strong>MARK</strong>: I use this macro to just output the name of class and method it was called from. Useful to know if a method was called.</p>
<pre><code>#define MARK	CMLog(@"%s", __PRETTY_FUNCTION__);
</code></pre>
<p><strong>START_TIMER</strong> and <strong>END_TIMER</strong>: These are for benchmarking a method:</p>
<pre><code>#define START_TIMER NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
#define END_TIMER(msg) 	NSTimeInterval stop = [NSDate timeIntervalSinceReferenceDate]; CMLog([NSString stringWithFormat:@"%@ Time = %f", msg, stop-start]);
</code></pre>
<p>Just put a <strong>START_TIMER</strong> at the beginning of the block to benchmark, and <strong>END_TIMER</strong> and the end, and you&#8217;ll get the timing.</p>
<pre><code>- (NSData *)loadDataFromURL:(NSString *)dataURL
{
  START_TIMER;
  NSData *data = [self doSomeStuff:dataURL];
  END_TIMER(@"loadDataFromURL");
  return data;
}
</code></pre>
<p>Will output:</p>
<pre><code>2009-01-05 10:31:37.943 MyApp[15283:20b] -[MyAppDelegate loadDataFromURL:]:loadDataFromURL Time = 3.636021
</code></pre>
<p>Now wrap all these declarations in your precompiled header, using a conditional flag. This flag will be set to 1 for Debug, and 0 for release, so your app will not fill the console with output.</p>
<pre><code>#if DEBUG==1
#define CMLog(format, ...) NSLog(@"%s:%@", __PRETTY_FUNCTION__,[NSString stringWithFormat:format, ## __VA_ARGS__]);
#define MARK	CMLog(@"%s", __PRETTY_FUNCTION__);
#define START_TIMER NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
#define END_TIMER(msg) 	NSTimeInterval stop = [NSDate timeIntervalSinceReferenceDate]; CMLog([NSString stringWithFormat:@"%@ Time = %f", msg, stop-start]);
#else
#define CMLog(format, ...)
#define MARK
#define START_TIMER
#define END_TIMER(msg)
#endif
</code></pre>
<p>Just add this to your <em>Debug</em> target setting</p>
<pre><code>OTHER_CFLAGS = -DDEBUG=1
</code></pre>
<p>and this to your <em>Release</em> target setting:</p>
<pre><code>OTHER_CFLAGS = -DDEBUG=0
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.coriolis.ch/2009/01/05/macros-for-xcode/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>HowTo fit text inside a UIWebView</title>
		<link>http://blog.coriolis.ch/2009/01/05/howto-fit-text-inside-a-uiwebview/</link>
		<comments>http://blog.coriolis.ch/2009/01/05/howto-fit-text-inside-a-uiwebview/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 22:25:14 +0000</pubDate>
		<dc:creator>Stephan</dc:creator>
				<category><![CDATA[cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[fit text]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[UIWebView]]></category>

		<guid isPermaLink="false">http://blog.coriolis.ch/?p=45</guid>
		<description><![CDATA[If, like me, you want to make sure your text will fit inside your UIWebView (without scrolling), you can implement the following javascript: &#60;script language=&#x27;javascript&#x27; type=&#34;text/javascript&#34;&#62; function adjustHeight(maxHeight) { elem = document.getElementById(&#34;sign&#34;); height = elem.offsetHeight; current_size = elem.style.fontSize.replace(&#x27;px&#x27;,&#x27;&#x27;)/1; while ((current_size-- &#62; 10) &#38;&#38; (height &#62; maxHeight)){ elem.style.fontSize = current_size + &#x27;px&#x27;; height = elem.offsetHeight; } [...]]]></description>
			<content:encoded><![CDATA[<p>If, like me, you want to make sure your text will fit inside your UIWebView (without scrolling), you can implement the following javascript:</p>
<pre><code>    &lt;script language=&#x27;javascript&#x27; type=&quot;text/javascript&quot;&gt;
    function adjustHeight(maxHeight) {
      elem = document.getElementById(&quot;sign&quot;);
      height = elem.offsetHeight;
      current_size = elem.style.fontSize.replace(&#x27;px&#x27;,&#x27;&#x27;)/1;
      while ((current_size-- &gt; 10) &amp;&amp; (height &gt; maxHeight)){
        elem.style.fontSize = current_size + &#x27;px&#x27;;
        height = elem.offsetHeight;
      }
    }
    &lt;/script&gt;
</code></pre>
<p>This script will reduce the size of the div until it fits the required height, but with a minimum font-size of 10px hardcoded.</p>
<p>Now in the html, call the adjustHeight function, passing it the id of the div to adjust:</p>
<pre><code>&lt;body onload=&quot;adjustHeight(48)&quot;&gt;
  &lt;div id=&quot;sign&quot; style=&quot;font-size:68px&quot;&gt;This is the text that fits&lt;/div&gt;
&lt;/body&gt;
</code></pre>
<p>The trick here is that to read the fontSize of a div, the font-size property should be inline. If the font-size is declared in the CSS, you&#8217;ll have to use another much more complex method, described in <a  href="http://developer.apple.com/internet/webcontent/styles.html">http://developer.apple.com/internet/webcontent/styles.html</a>.</p>
<p>PS: Instead of reading the font-size from the div, I could have hardcoded its value in the script, and avoided much trouble.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.coriolis.ch/2009/01/05/howto-fit-text-inside-a-uiwebview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bring any window to the front</title>
		<link>http://blog.coriolis.ch/2008/03/04/bring-any-window-to-the-front/</link>
		<comments>http://blog.coriolis.ch/2008/03/04/bring-any-window-to-the-front/#comments</comments>
		<pubDate>Tue, 04 Mar 2008 17:39:24 +0000</pubDate>
		<dc:creator>Stephan</dc:creator>
				<category><![CDATA[cocoa]]></category>
		<category><![CDATA[CGWindow]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[window]]></category>

		<guid isPermaLink="false">http://blog.coriolis.ch/2008/03/04/bring-any-window-to-the-front/</guid>
		<description><![CDATA[A question was asked on the apple dev mailing list on how to bring a window with a certain name to the front. Since I was already working on how to get the list of windows on the screen, I just added some AppleScript to bring the window to the front. This snippet doesnt use [...]]]></description>
			<content:encoded><![CDATA[<p>A question was asked on the apple dev mailing list on how to bring a window with a certain name to the front.</p>
<p>Since I was already working on how to get the list of windows on the screen, I just added some AppleScript to bring the window to the front.</p>
<p>This snippet doesnt use any undocumented techniques, it uses the CGWindow API introduced in Leopard.</p>
<p>I dont consider this code to be production ready, but it works.</p>
<p>Of course, <a  href="http://en.wikipedia.org/wiki/There_is_more_than_one_way_to_do_it">TIMTOWTDI</a> <img src='http://blog.coriolis.ch/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>See the code after the jump:<br />
<span id="more-6"></span></p>
<pre><code>
- (BOOL) bringWindowWithNameToFront:(NSString *) windowName
{
// Try to find a window named "windowName", and if found,
// bring it to the front
// Stephan Burlot 4/3/08

  CFArrayRef            windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
  ProcessSerialNumber   myPSN = {kNoProcess, kNoProcess};
  BOOL                  returnValue = FALSE;

  for (NSMutableDictionary *entry in (NSArray *)windowList) {
    NSString *currentWindow = [entry objectForKey:(id)kCGWindowName];
    if ((currentWindow != NULL) &amp;&amp; ([currentWindow isEqualToString:windowName]))
    {
      NSString *applicationName = [entry objectForKey:(id)kCGWindowOwnerName];
      int pid = [[entry objectForKey:(id)kCGWindowOwnerPID] intValue];
      GetProcessForPID(pid, &amp;myPSN);

      NSString *script= [NSString stringWithFormat:@"tell application \"System Events\" to tell process \"%@\" to perform action \"AXRaise\" of window \"%@\"\ntell application \"%@\" to activate\n", applicationName, windowName, applicationName];
      NSAppleScript *as = [[NSAppleScript alloc] initWithSource:script];
      [as compileAndReturnError:NULL];
      [as executeAndReturnError:NULL];  // Bring it on!
      [as release];
      returnValue = TRUE;
    }
  }

  CFRelease(windowList);
  return returnValue;
}
</code></pre>
<p>Inspiration camed from the following codes:</p>
<ul>
<li><a  href="http://developer.apple.com/samplecode/SonOfGrab">SonOfGrab sample from Apple</a></li>
<li><a  href="http://desktopmanager.berlios.de/">Desktop Manager source</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.coriolis.ch/2008/03/04/bring-any-window-to-the-front/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: www.coriolis.ch @ 2012-02-04 23:05:25 -->
