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!

Mustafa
  • 20,504
  • 42
  • 146
  • 209

2 Answers2

3

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
  • 58,279
  • 31
  • 157
  • 188
  • The original dimensions are 2272 × 1704 – Mustafa Feb 12 '10 at 14:17
  • I guess you maybe right about the compression and decompression. How can i avoid this decompression? – Mustafa Feb 12 '10 at 14:21
  • 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 Feb 12 '10 at 18:52
  • 1
    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 Feb 13 '10 at 08:23
  • Just out of interest and curiosity, where did you come across that method of working out the memory size of an image? – Jasarien Feb 13 '10 at 14:32
0

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...