views:

125

answers:

3

Hi everyone,

I've been having a problem to show large images on a scrollview, the images are 2,4 - 4,7 MB. It runs fine on the 3GS and on the simulator. But whenever I try to run on a 3G or iPod Touch 2G, it crashes.

I searched across the web and found the "imageNamed is evil" article. Ok I changed all my image calls to imageWithContentsOfFile: but it still crashes, the only different that I see is that now images are deallocated after I leave the view just fine. But the memory usage is still very high.

Here is a screenshot of Instruments. alt text First peak is a video I show at startup, then the tableview shows a lot of images, until then no problems.

alt text When I enter a 1,1mb - 2576 x 1000 picture

alt text When I enter a 4,8mb - 7822 x 1000 picture

By the way the app was tested on iOS 4 and 3.1.2

Do you have any tips? Because this problem is driving me nuts.

Since now thanks a lot!

+1  A: 

The GPUs in the iPhone 3G and iPod Touch 1G/2G only have room for 1k by 1k textures, which seems to also be used for 2D image rendering. Anything larger needs to be tiled to fit the hardware graphics renderer.

The 3GS and newer have a different GPU which can support larger textures, and thus images.

hotpaw2
I was wondering how the iphone native photo album does, do you think is this way?
Lookez
Could you provide me any code to tile the image into other parts?
Lookez
If you've got access to Apple's developer site, login here: https://developer.apple.com/videos/wwdc/2010/ and download the sample code and session video 104 via iTunes (credit to `GreginYEG` for mention of this session).
petert
A: 

I recently ran into the same problem while testing an app on my 3G. I ended up scaling down any images larger than a maximum number of pixels (I found that 2 million pixels seemed to work reliably on my 3G, but hotpaw2's answer seems to suggest that 1 million pixels may be a safer bet).

UIImage *image = // ...;
if (image.size.width * image.size.height > MAX_PIXELS) {
    // calculate the scaling factor that will reduce the image size to MAX_PIXELS
    float actualHeight = image.size.height;
    float actualWidth = image.size.width;
    float scale = sqrt(image.size.width * image.size.height / MAX_PIXELS);

    // resize the image
    CGRect rect = CGRectMake(0.0, 0.0, floorf(actualWidth / scale), floorf(actualHeight / scale));
    UIGraphicsBeginImageContext(rect.size);
    [image drawInRect:rect];
    UIImage *imageToDraw = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // imageToDraw is a scaled version of image that preserves the aspect ratio
}

Apple also provides an example of developing a photo gallery app that uses CATiledLayer to tile very large images. Their example uses images that have been sliced into tiles of the appropriate sizes in advance. It is possible to slice the images into tiles on the fly in your iOS app, but doing so is quite slow on the device. Check out session 104 of this year's WWDC for the PhotoScroller example.

GregInYEG
Thanks a lot, I just checked the photo gallery you said. I managed to render the 4mb image on my iPhone on Safari, and now what I'll try to do is put a webview instead a scrollview, because I think Safari uses the same algorithm found in the native photo gallery. *fingers crossed*
Lookez
I also experienced problems with large images. At the moment, I resize images to 1024x768 as these dimensions suffice for my application. Performance has increased and I haven't experienced any crashes since.
bare_nature
A: 

I ended up using UIWebView, I had to use smaller (in MB, not dimensions) images but it's working on, the only problem Is that I can't zoom... Using the scalePageToFit I can zoom but the image fits horizontally not vertically, that sux.

I will try to keep this issue updated.

If someone has this problem I would advice to use webview or an adapter version of three20 image viewer that handles memory better.

Lookez