Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the fastest way to load big image on iPhone?

Should I use UIImage or CGImage ? Png or Jpg ? I've read the doc and tried different things but did not notice significant improvement. Loading an image can take 1 good second which seems slow

like image 315
CodeFlakes Avatar asked Jan 22 '23 17:01

CodeFlakes


2 Answers

UIImage is just an ObjC wrapper of CGImage, so they're the same.

From the SDK doc:

You should avoid creating UIImage objects that are greater than 1024 x 1024 in size. Besides the large amount of memory such an image would consume, you may run into problems when using the image as a texture in OpenGL ES or when drawing the image to a view or layer. This size restriction does not apply if you are performing code-based manipulations, such as resizing an image larger than 1024 x 1024 pixels by drawing it to a bitmap-backed graphics context. In fact, you may need to resize an image in this manner (or break it into several smaller images) in order to draw it to one of your views.

If you have a huge image, you could try to use a UIWebView to reduce memory consumption.


The time to load an image has 2 parts: the time to decompress the image (relevant to choosing JPG or PNG) and the time to render the image.

For decompressing, I'd suggest you profile the simple statement

[UIImage imageWithContentsOfFile:@"/path/to/your/image.jpg"];
like image 160
kennytm Avatar answered Jan 25 '23 05:01

kennytm


It is faster for the iPhone to load PNGs than JPGs because PNGs are optimized when bundled in your application (although, not loaded from remote).

An except from Addison Wesley's iPhone Cookbook:

"Xcode automatically optimizes your PNG images using the pngcrush utility shipped with the SDK. (You'll find the program in the iPhoneOS platform folders in /Developer. Run it from the command line with the –iphoneswitch to convert standard PNG files to iPhone- formatted ones.) For this reason, use PNG images in your iPhone apps where possible as your preferred image format."

Also, PNG is a lossless format, and JPGs are lossy. Apple chose this format for these reasons.

-Kevin

like image 20
Kevin Elliott Avatar answered Jan 25 '23 05:01

Kevin Elliott