views:

932

answers:

2

Background:

Inspired from Apple's sample code ScrollViewSuite, I've created a view controller class that shows picture thumbnails and one selected picture. The hierarchy of controls for the "selected" picture is something like this:

--> UIView
    --> UIScrollView
        --> UIImageView

Following code is used to put the UIScrollView onto the view:

imageScrollView = [[UIScrollView alloc] initWithFrame:frame];
[imageScrollView setBackgroundColor:[UIColor clearColor]];
[imageScrollView setDelegate:self];
[imageScrollView setBouncesZoom:YES];
[[self view] addSubview:imageScrollView];

... and following code is used to configure and add UIImageView to the UIScrollView:

// Custom method to return a UIImage from a URL string
UIImage *image = [UIImage newImageWithContentsOfURL:imageURL];  

// first remove previous image view, if any
[[imageScrollView viewWithTag:MAIN_IMAGE_TAG] removeFromSuperview];

// set the new image view
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setDelegate:self];
[imageView setTag:MAIN_IMAGE_TAG];
[imageScrollView addSubview:imageView];
[imageScrollView setContentSize:[imageView frame].size];

// choose minimum scale so image width fits screen
float minScale  = [imageScrollView frame].size.width / [imageView frame].size.width;
[imageScrollView setMinimumZoomScale:minScale];
[imageScrollView setZoomScale:minScale];
[imageScrollView setContentOffset:CGPointZero];

// clear memory
[imageView release];
imageView = nil;

[image release];
image = nil;

Here's the category method I've used to get UIImage using URL string:

+ (UIImage *)newImageWithContentsOfURL:(NSString *)imageURL {   
    NSURL *url = [[NSURL alloc] initWithString:imageURL];
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];
    UIImage *image = [[UIImage alloc] initWithData:data];
    [data release];
    [url release];

    return image;
}

Problem: The affect of loading a jpeg image of size 110 Kb (approx.) is that the real memory of the application jumps from 12 MB (approx.) to 38 MB (approx.). I was baffled when i first saw this. How is this possible? Uh, and the end result: Application crashes on iPhone 3G (occasionally).

Note that the memory readings were taken using Memory Monitor tool in Instruments - while testing the application on the device (not the simulator). Also note that Instruments show no memory leaks, and Static Analyzer doesn't point to anything suspicious either.

I need help!

+2  A: 

Could it have something to do with the fact that a jpeg is compressed. It could be being uncompressed when being displayed, hence the huge jump in memory.

What are the dimensions of the image at 1:1 scale?

Jasarien
The original dimensions are 2272 × 1704
Mustafa
I guess you maybe right about the compression and decompression. How can i avoid this decompression?
Mustafa
That is a huge image to be using on the iPhone. Can't you resize it? That's why it's taking up so much memory, that's like a 3 megapixel image.
Jasarien
Here's something i didn't knew: File size does not equal memory size. For images it doesn't matter what the file size is. The only size that truly matters is the height and width. Memory = width * height * 4 (2272 x 1704 * 4 = 15,485,952 bytes). All i had to do was to scale down the image before putting it in the UIScrollView (and put it on display). Thanks for pointing me in the right direction.
Mustafa
Just out of interest and curiosity, where did you come across that method of working out the memory size of an image?
Jasarien
A: 

Surely it must be something other than the jpeg which is making it use so much memory & crash - I have a png which is 15200x250 px and it scrolls beautifully...

Franklyn Franklyn Weber