views:

134

answers:

1

I am working on an app that downloads images from a web site for display on iOS devices. The app runs fine on an iPad, but when run on an iPhone 3G (the only other physical device at my disposal for testing), it crashes when loading large images. As suggested elsewhere, it seems that 1024 x 1024 is about the largest size image it can consistently handle.

To attempt to workaround this, I've added code to resize images larger than 1024 wide or 1024 high (code taken from here):

if (image.size.width > 1024 || image.size.height > 1024) {
    // resize the image
    float actualHeight = image.size.height;
    float actualWidth = image.size.width;
    float imgRatio = actualWidth/actualHeight;
    float maxRatio = self.frame.size.width/self.frame.size.height;

    if(imgRatio!=maxRatio) {
        if(imgRatio < maxRatio) {
              imgRatio = self.frame.size.height / actualHeight;
              actualWidth = imgRatio * actualWidth;
              actualHeight = self.frame.size.height;
        } else {
              imgRatio = self.frame.size.width / actualWidth;
              actualHeight = imgRatio * actualHeight;
              actualWidth = self.frame.size.width;
        }
    }
    CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
    UIGraphicsBeginImageContext(rect.size);
    [image drawInRect:rect];
    imageToDraw = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

However, resizing very large images (for example, one that is about 3500 x 2500 pixels) still crashes the app.

Is there any other mechanism that I can use to safely resize such large images, or other mechanisms that I can use for displaying them without resizing? I don't have the ability to tile the images outside of my app for use in a CATiledLayer, as I don't control the source of the images.

Thanks!

Edit: after some more debugging, I found the source of the crash (see answer below). However, I'd still welcome any advice regarding better ways to allow viewing of very large images on an iOS device without resizing the images.

A: 

I just noticed that the picture in question had the same aspect ratio as the screen (which was in landscape orientation when the crash occurred). Since imgRatio == maxRatio for this image in the above code, no resizing occurred.

I modified the above code to look like this:

if (image.size.width > 1024 || image.size.height > 1024) {
    // resize the image
    float actualHeight = image.size.height;
    float actualWidth = image.size.width;
    float imgRatio = actualWidth/actualHeight;
    float maxRatio = self.frame.size.width/self.frame.size.height;

    if(imgRatio < maxRatio){
        imgRatio = self.frame.size.height / actualHeight;
        actualWidth = imgRatio * actualWidth;
        actualHeight = self.frame.size.height;
    }
    else{
        imgRatio = self.frame.size.width / actualWidth;
        actualHeight = imgRatio * actualHeight;
        actualWidth = self.frame.size.width;
    }
    CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
    UIGraphicsBeginImageContext(rect.size);
    [image drawInRect:rect];
    imageToDraw = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

Now it works for the image that was previously crashing the iPhone.

GregInYEG
it will be awesome if you will write the whole method and not just a part of it. thanks
Dmitry
What more are you looking for? This is all the code that pertains to the image resizing, which was the problematic code. The other code in the method is not related.
GregInYEG