tags:

views:

28

answers:

1

i am getting a crash issue(it is due to low memory) while picking up the a large image (3.5 MB) from the photo library in iphone 3G. The problem is only with iphone 3G while it is working fine in iPhone 3GS and iPhone 4. I am saving this image to my iphone library from my email, and it seems that the image saved to the library is not getting optimized. Is this a bug ? Can anybody suggest me a solution. It is possible to restrict the user from picking very large image from the library in iphone 3G. I tried picking up this large image using the sample application given by apple, and it is crashing even with that.

A: 

Is the crash happening when you pick the image, or when you try to put the image in a UIImageView? If the latter, you may need to scale the image to a smaller resolution before placing it in the UIImageView. I have seen recommendations that image sizes greater than 1024 x 1024 should be avoided in UIImageView.

You can do this using code like the following:

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();
}

Note that imageToDraw is a UIImage * declared outside of this code, and is the final image that can be put into the UIImageView. This code came from a method in a custom view that I wrote, so where you see self.frame in the code, you can replace with whatever frame you are scaling the image into.

GregInYEG