开发者

Major speed issues with imageWithContentsOfFile

In my application I'm creating a large image dynamically and then loading it up for display in my image explorer clas开发者_开发知识库s. Because I can't add new images to the bundle at run time, it seems I have to use imageWithContentsOfFile - however, this gives me major speed issues further down the line.

The way my image explorer works is that it takes in an image, splits it up into tiles, caches those tiles and then only loads those tiles into memory for display that need to be shown on the screen. Using a bunch of NSLogs, I've managed to find out where all the slowdown is. It's not in the imageWithContentsOfFile function itself, it's when I try to call this line:

CGContextDrawImage(context_ref,
                   CGRectMake(0, 0, imgWidth, imgHeight), tileImage);

This is when I'm writing the tile to the cache file. tileImage is a CGImageRef that is returned from CGImageCreateWithImageInRect, which is how I get subsets of my larger image to save separately.

The odd thing is that splitting up a large image this way takes about 45 seconds (!), but when I split up an image from the bundle using imageNamed rather than imageWithContentsOfFile, it takes only about 2 seconds.

Anyone have any ideas? Thanks in advance :)


I think U should split up your image. Because, CGContextDrawImage will take fully loaded "tileImage".

If your "tileImage" size is 8 MB, your app must load 8MB data to memory. It takes long time for loading. and It may create memory issue and so on.

If you want to use a single big image and you can wait for loading, there is solution that U can use another thread.

It can avoid to UI lock during loading a big image.

An 8MB JPG image will use over 8MB memory, UIImage should use noncompressed format for fast drawing.


imageNamed uses caching, and may reduce the amount of scaling.

UIImage is immutable. imageNamed may note this and return a reference to a cached image, rather than loading and creating a new image... wherever you load your image.

if you create an images, you can setup your own (in memory) caching scheme and pass references in many cases. then purge the cache when you receive a memory warning.

if you need to scale the image and the size is static, determine the size to draw, and create a UIImage using imageWithCGImage:scale:orientation: -- or you can approach the problem in a similar way using CoreGraphics apis directly too.

beyond that, hold onto/reuse what you need, and use a profiler to balance your allocations and to measure timings.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜