views:

66

answers:

2

I'm loading a 2400x1845 png image into a scroll view. The program crashes out of memory, is there a better way to handle this? mapScrollView is an UIScrollView in IB, along with a couple of UIButtons.

-(void)loadMapWithName:(NSString *)mapName
    {

        NSString* bundlePath = [[NSBundle mainBundle] bundlePath];
        UIImage *image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/path/%@", bundlePath, [maps objectForKey:mapName]]];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        CGSize imgSize = image.size;
        mapScrollView.contentSize = imgSize; 

        [mapScrollView addSubview:imageView];
        [imageView release];

        [self.view addSubview:mapScrollView];

    }
+2  A: 

UIImages are limited to 1024x1024 on the iPhoneOS. If you want to get around this, I think the only way is to make use of a CATiledLayer.

Martin Cote
Thanks for the answer. There's another approach that involves dividing the image into tiles. Can't remember where I saw it though. Thanks
Jordan
+1  A: 

I don't know (exactly) how accurate this is, but I often use 64 MB physical memory as a guideline when targeting the iPhone OS (therefore attempting to restrict usage to half that amount). Having said that: The raw allocation size is not the issue.

You may be able to work around the issue by using CGImageRef (the lower level opaque image type) -- essentially wrapped by UIImage.

As another poster has stated, the example exceeds the maximum allowed image size. No device (iPhone/iPod) supports images of that scale, so resizing/dividing may be worth consideration.

Justin