App Crashing when using large images on iOS 4.0

后端 未结 3 1103
闹比i
闹比i 2020-12-15 14:03

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 3

3条回答
  •  失恋的感觉
    2020-12-15 14:44

    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.

提交回复
热议问题