Application crashes when using large images on iOS 4.0

I had a problem with displaying large images on scrollview, images 2.4 - 4.7 MB. It works great on 3GS and on the simulator. But whenever I try to launch 3G or iPod Touch 2G, it crashes.

I searched the net and found the article "imageNamed is evil". Ok, I changed all my image images to imageWithContentsOfFile: but it still crashes, only the different ones that I see are that now the images are freed after I left the view just fine. But memory usage is still very large.

Here is a screenshot of the tools. alt text The first peak is the video that I show at startup, then a lot of images are displayed in the table view, so far no problems.

alt text When I enter the image 1,1mb - 2576 x 1000

alt text When I enter a 4.8mb image - 7822 x 1000

By the way, the application was tested on iOS 4 and 3.1.2

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

Thank you very much since then!

+4
source share
3 answers

I ended up using UIWebView, I had to use smaller (in MB, not dimensions) images, but it works, the only problem is that I can’t zoom ... Using scalePageToFit I can scale, but the image does not fit horizontally vertically, this is sux.

I will try to keep this issue updated.

If anyone has this problem, I would recommend using a webview or a version of a 3D image adapter that handles memory better.

0
source

I recently encountered the same problem when testing an application on my 3G. I ended up scaling any images that exceed the maximum number of pixels (I found that 2 million pixels seem to work reliably on my 3G, but hotpaw2's answer seems to suggest that 1 million pixels can be more secure).

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 development of a photo gallery application that uses the CATiledLayer to tile very large images. In their example, images that were previously cut into tiles of appropriate sizes are used. You can cut images into tiles on the fly in the iOS application, but on the device rather slowly. End this year's 104 WWDC session for the PhotoScroller example.

+4
source

The iPhone 3G and iPod Touch 1G / 2G GPUs have room for 1k by 1k textures, which seems to be also used for rendering 2D images. All that needs to be done must be tiled to match the hardware graphics.

3GS and newer has another GPU that can support large textures and therefore images.

+2
source

All Articles