1

I have a very, very large UIView, which uses a CATiledLayer.

I need to render a lot of small images onto it.

These small images are stored in one large image (1024x1024). They may have transparency.

At the moment, I use CGImageCreateWithImageInRect on the large image to create CGImageRefs for each small image that I need.

Later, within drawRect, I call CGContextDrawImage(context, rect, smallImage); to display each of these small images, as required.

According to Instruments' Time Profiler, however, there's a very large bottleneck at CGContextDrawImage (274ms), and I wish to reduce that as best I can (short of using OpenGL).

My question is: how? Is there an alternative to CGContextDrawImage which does not require scaling? Is using CGImageCreateWithImageInRect the right way to go about it?

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
PhilipG
  • 113
  • 6
  • Few questions - how big is the 'one large image'? are you using [UIImage imageNamed:@"yourImage"]? And also is your subclass overriding the 'layerClass' class method? Finally you should check this link out: http://www.cimgf.com/2011/03/01/subduing-catiledlayer/ – fatuous.logic Jan 15 '14 at 10:43
  • Can you please provide some clarification on what you are trying to achieve with the pattern you are following? You can have all the images cropped and added to the project before hand, which will reduce the time. – Kamran Khan Jan 15 '14 at 11:08
  • @Neuronical the one large image is 1024x1024. In OpenGL, it would be the texture I would use to blit all the little images, which are typically around 32 pixels in width and height. Yes, for my really big UIView, I am overriding 'layerclass', returning `[CATiledLayer class]`. Thanks for the link, but I should point out that the big image is just an image map for the little images, which are distributed over the extremely large UIView and not tiled together. – PhilipG Jan 15 '14 at 11:30
  • @kamran imagine a project to draw a folder window in Finder, where the folder contains tens of thousands of files. Each file has a non-unique icon associated; if you were to paste the unique icons together, they would fit a bitmap of size 1024x1024. This is similar to what I'm doing. The CATiledLayer is the folder window. I need to blit the icons as efficiently as possible. `CGContextDrawImage` has been identified as slow by Instruments. So what's the solution? Hope that clarifies the problem. – PhilipG Jan 15 '14 at 11:36
  • yes, Thanks. Let me think about it. There are cocos2d methods that do this sort of thing, but I am not sure if they can be used directly in your project without having to create a cocos2d project base. Please take a look at this link http://www.raywenderlich.com/32045/how-to-use-animations-and-sprite-sheets-in-cocos2d-2-x, http://www.spritehelper.org/app/SpriteHelper_API_Documentation/DocumentationCocos2d/COCOS2D_loadingAndDisplayingSpritesCocos2d.htm . I will try to think of something and let you know. – Kamran Khan Jan 15 '14 at 11:41
  • Try this c library http://stackoverflow.com/questions/4906736/working-with-images-in-c-or-c It will crop and save images locally, which you can then load in to the UIImageView and place it on the UIView. May be this will work faster. – Kamran Khan Jan 15 '14 at 11:56
  • Thanks for the links, but I don't think that they're much help to my particular problem unfortunately. I don't use UIImageViews. If I did then I'd need a way to manage having hundreds or even thousands of them in the big UIView. – PhilipG Jan 15 '14 at 12:12
  • So you are drawing it directly on layer? Why don't you add them as a subview or sublayer and devise a mechanism like the UITableview where you can load only the items that are visible on the screen (in the uiview). And when the user scrolls up or down you change the images. It will save memory and might reduce the time delay. – Kamran Khan Jan 15 '14 at 12:53
  • The time before when I tried such a system, performance was a lot worse. Maybe my implementation was to blame. If nobody else has any suggestions, I'll try such an approach again. – PhilipG Jan 15 '14 at 13:07

0 Answers0