{"id":262,"date":"2010-12-10T10:21:59","date_gmt":"2010-12-10T15:21:59","guid":{"rendered":"http:\/\/www.jaimerios.com\/?p=262"},"modified":"2010-12-10T10:21:59","modified_gmt":"2010-12-10T15:21:59","slug":"another-cocoa-technology-i-had-to-battle-with","status":"publish","type":"post","link":"https:\/\/jaimerios.com\/?p=262","title":{"rendered":"Another Cocoa technology I had to battle with"},"content":{"rendered":"<p>So, I was messing around with a Cocoa app I created to test out some imaging code I had created. The point of this code was to deliver a really large image in smaller tiles and the Cocoa app would take those images and stitch them together into one large image. Now, the code I wrote delivered these small tiles quickly, and the point of the code was to handle the tiles, not to deliver one big image, so I used NSImage to put these tiles together.<\/p>\n<p>Now&#8230; that didn&#8217;t work out too well. After many attempts, debugging and coding permutations, I realized that NSImage might not have been the best choice for drawing for one reason: it&#8217;s not that fast.<\/p>\n<p>So, on to the web. After some digging around, and some patient reading, I found that CGImage would be the better way to go. Great! Now I have to admit, all of the projects I have done in the past were with whole images, not diced up ones. So, looking at the Cocoa API, I wondered, how do I draw just one Rect.<\/p>\n<p>Now, before I go any further, I would like to point out something. I don&#8217;t write about anything unless I had a hard time finding information about something and I couldn&#8217;t find it on the web. And to my surprise, I really couldn&#8217;t find that much information about tiling images.<\/p>\n<p>Okay, so, back to my problem. I implemented tiled drawing, and it wasn&#8217;t too hard to do, but then I started having some problems. My original code was expecting only one tile at a time to be drawn in. However, there were sections of the image that were not being drawn.<\/p>\n<p>My <code>- (void)drawRect:(NSRect)inrect<\/code> method was being called every time, or at least from what I could see, but, tiles were still missing.<\/p>\n<p>So what gives?<\/p>\n<p>Well, by accident I found out that you can get more than one tile at a time. Cocoa tries to make the drawing as efficient as possible if it detects that it can draw two or more tiles at the same time. So, I also had to use:<\/p>\n<p><code>- (void)getRectsBeingDrawn:(const NSRect **)rects count:(NSInteger *)count<\/code><\/p>\n<p>This method tells me how many tiles were given to me, so instead of only drawing one tile, I was now able to draw many tiles:<\/p>\n<pre LANG=\"OBJC\">\n- (void)drawRect:(NSRect)inrect\n{\n\t\/* Prevent anyone else from trying to update the view by locking our mutex*\/\n\t[self lockMyView];\n\n\t\/* Get the array of rects Cocoa wants us to draw *\/\n\tconst NSRect* \tarrayOfRects;\n    NSInteger\t\tcountOfTiles;\n\t[self getRectsBeingDrawn:&arrayOfRects tileCount:&countOfTiles];\n\n\tif (countOfTiles==1)\n\t{\n\t\t\/* We only have one tile to draw... easy! *\/\n\t\t[self drawMyTile:(*arrayOfRects)];\n\t}\n\telse\n\t{\n\t\t\/* We have more than one tile to draw. *\/\n\t\tfor (int i = 0; i <= countOfTiles; i++ )\n\t\t{\n\t\t\tNSRect currentRect;\n\t\t\tcurrentRect.origin.x = arrayOfRects[i].origin.x;\n\t\t\tcurrentRect.origin.y = arrayOfRects[i].origin.y;\n\t\t\tcurrentRect.size.width = arrayOfRects[i].size.width;\n\t\t\tcurrentRect.size.height = arrayOfRects[i].size.height;\n\t\t\t\n\t\t\t[self drawMyTile:currentRect];\n\t\t} \n\t}\n\n\t\/* It is now safe to allow view updates *\/\n\t[self unlockMyView];\n}\n<\/pre>\n<p>Alright! Another problem solved!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, I was messing around with a Cocoa app I created to test out some imaging code I had created. The point of this code was to deliver a really large image in smaller tiles and the Cocoa app would take those images and stitch them together into one large image. Now, the code I &#8230; <a title=\"Another Cocoa technology I had to battle with\" class=\"read-more\" href=\"https:\/\/jaimerios.com\/?p=262\" aria-label=\"Read more about Another Cocoa technology I had to battle with\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[112,122,324,450],"class_list":["post-262","post","type-post","status-publish","format-standard","hentry","category-coding","tag-cgimage","tag-cocoa","tag-nsimage","tag-tiles"],"_links":{"self":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=262"}],"version-history":[{"count":0,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/262\/revisions"}],"wp:attachment":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}