App Crashing when using large images on iOS 4.0

2019-01-23 09:21发布

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!

3条回答
够拽才男人
2楼-- · 2019-01-23 09:57

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.

查看更多
做个烂人
3楼-- · 2019-01-23 10:03

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.

查看更多
Summer. ? 凉城
4楼-- · 2019-01-23 10:11

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.

查看更多
登录 后发表回答