Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-05-2008, 06:23 AM   #1 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 37
Question An efficient way to draw a small part of a large image?

Hello,

we've been writing a program for quite a long time now. And we won't get it finished because we have trouble drawing a small part of a large image file:
Our Xcode project holds a large image (.gif, about 4700x3100 pixels, 6MB). The file cannot be made smaller because we need this resolution. We have to draw a small part of it on the screen again and again, on avarage every 20 seconds. Our program does some calculation what part of the large image is relevant (actually it calculates the origin and the size of a small rectangle within the large picture). Then we draw this part on the screen.

For the drawing part we are using Quartz:
Code:
UIImage* largePic = [UIImage imageNamed:@"largePic.gif"];

CGImageRef _imageRef = CGImageCreateWithImageInRect(largePic.CGImage, rectOfPicToLoad);
CGContextDrawImage(_context, CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height), _imageRef);
CGImageRelease(_imageRef);
The drawing takes about 300ms. As we don't want the user to expirience any delay we put the calculation and the drawing part into a separate thread.

It works all well in the iPhone Simulator. Our program does what it should, it is performant and Instruments tells us that the overall size in memory was 2.5MB. Perfect! What is not perfect is the fact that out program crashes on the device. It crashes after a short, but not predictable time. "Program exited with status value:101." The device console logged it: <10% memory available. Therefore it got killed. A crashreport was generated:
Code:
PID:1421  RPRVT:55.3M  RSHRD:7.84M  RSIZE:56.2M Command:REDproj
Also, the Apple UIImage Reference says programmers shouldn't use UIImage for images larger than 1024x1024 pixels. I guess the UIImage class does some caching which doesn't get displayed in Instuments (remember that our program does not crash in iPhone Simulator!). So we replaced the UIImage part with:
Code:
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
NSString *path = [NSString stringWithString:[[NSBundle mainBundle] pathForResource:@"largePic" ofType:@"gif"]];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithString:path]];
CGDataProviderRef dp = CGDataProviderCreateWithURL((CFURLRef) url);
CGImageRef _imageRef = CGImageCreate(4678, 3090, 8, 8*4, 4678*4, cs, kCGImageAlphaLast, dp, NULL, NO, kCGRenderingIntentDefault);
CGDataProviderRelease(dp);
CGColorSpaceRelease(cs);
_imageRef = CGImageCreateWithImageInRect(_imageRef, rectOfPicToLoad);
CGContextDrawImage(_context, CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height), _imageRef);
CGImageRelease(_imageRef);
It also crashes because of too less memory after some while but with the difference that it takes about 1 minute to draw the pictures . Unacceptable.

Who knows a strategy to load a small rectangle out of a very large picture without crashes and which is as fast as the UIImage class?
Norbert is offline   Reply With Quote
Old 12-05-2008, 07:34 AM   #2 (permalink)
Coder for Life
 
Karl Kraft's Avatar
 
Join Date: Sep 2008
Location: Austin, TX
Posts: 76
Send a message via AIM to Karl Kraft
Default Do the opoosite, make large image from small ones

Quote:
Originally Posted by Norbert View Post
Our Xcode project holds a large image (.gif, about 4700x3100 pixels, 6MB). The file cannot be made smaller because we need this resolution.
No, you don't need this resolution. The phone can't display an image larger than 480x320, since that is the size of the screen. The best way to fix your problem is by breaking it into tiles no larger than 480x320, and then drawing the tiles that would be visible at any given time.

Right now your image would consume over 58 million bytes of memory on the phone once it has been decompressed, which is neeed to make it displayable. The 6MB size you quote is the compressed size (GIFs are compressed), UIKit needs to decompress the GIF into RGBA (GIFs can have transparency) in order to diplay on screen. 4700*3100*4(RGBA) = 58,280,000.



By tiling it into 480x320 segments, you would never need more than 4 segments loaded at a time, or 2.4MB of memory (480*320*4*4) = 2,457,600. If the image doesn't need to be transparent and you switch it to a PNG you could get that down to 1.8MB. And depending on the particulars of your application you could get it down a bit more.
Karl Kraft is offline   Reply With Quote
Old 06-19-2009, 10:56 AM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 133
Default

So to achieve this would you use something like the Tiling example from Apple or a different method?

I am working thru their example but am finding it a bit of an uphill struggle as they have so much other stuff mixed in with it, also not overly helpful comments!
ghanalupo is offline   Reply With Quote
Reply

Bookmarks

Tags
101, cache, quartz, uiimage

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 257
22 members and 235 guests
ADY, bookesp, ckgni, dacapo, Dani77, DarkAn, Davey555, Desert Diva, glenn_sayers, HemiMG, iDifferent, jakerocheleau, JasonR, LEARN2MAKE, nobre84, prchn4christ, Rudy, ryantcb, Speed, themathminister, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,766
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:39 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0