开发者

ipad scrollview causing malloc of 40x the image's size

Bear with me as I am new to obj-c but I have a UIScrollView and a segmented button that switches between 2 images that are presented in the scrollview. Each image is large and roughly 500Kb, but they are each causing allocations of 20+ MB and crashing my app.

     - (void)viewDidLoad {
 [bvpiscrollview setContentSize:CGSizeMake(768, 2484)];
 UIImage *loadBvImage = [UIImage imageNamed:@"bpi_1536x4965v2.png"];
 bvpiimg.image = loadBv开发者_JAVA技巧Image;
 [loadBvImage release];
 [super viewDidLoad];
}
-(UIView *)viewForZoomingInScrollView:bvpiscrollview{
 return bvpiimg;
}

-(IBAction)pidonebutton{
 [self.view removeFromSuperview];
}

-(IBAction)segmentedControlIndexChanged{
 switch (self.segmentedControl.selectedSegmentIndex)
 {
  case 0:
   [bvpiscrollview setContentSize:CGSizeMake(768, 2484)];
   UIImage *loadBvImage = [UIImage imageNamed:@"bpi_1536x4965v2.png"];
   bvpiimg.image = loadBvImage;
   [loadBvImage release];
   break;
  case 1:
   [bvpiscrollview setContentSize:CGSizeMake(768, 2556)];
   UIImage *loadMpImage = [UIImage imageNamed:@"mpi_1536x5110v2.png"];
   bvpiimg.image = loadMpImage;
   [loadMpImage release];
  default:
   break;

 }
}
- (void)dealloc {
 [bvpiimg release];
 [bvpiscrollview release];
 [segmentedControl release];
    [super dealloc];
}
@end


A 500k RGBA image would be about 350 pixels square. A 1536 x 5110 pixel RGBA image is about 31.4 MB. You seem to be confusing image size with file size.

The largest size supported across all iOS devices is 1024 pixels on a side, although some support larger dimensions. Try cutting up your large images into smaller tiles before adding them to a view in the view hierarchy. You can cut the image up at runtime with a series of calls to CGImageCreateWithImageInRect. I usually choose 512 pixel or smaller tiles.


Some background information for you taken from here:

On the speed front, there is a general misunderstanding of what is going on. The biggest thing that +imageNamed: does is decode the image data from the source file, which almost always significantly inflates the data size (for example, a screen sized PNG file might consume a few dozen KBs when compressed, but consumes over half a MB decompressed - width * height * 4). By contrast +imageWithContentsOfFile: will decompress that image everytime the image data is needed. As you can imagine, if you only need the image data once, you've won nothing here, except to have a cached version of the image hanging around, and likely for longer than you need it. However, if you do have a large image that you need to redraw often, then there are alternatives, although the one I would recommend primarily is to avoid redrawing that large image :).

I'm guessing when the image is loaded, especially given the very large size from your code snippet above, that it's using up quite a bit of memory.

You could try turning off image optimization like was done here or try a different image format other than PNG.


Don't load such big images with [UIImage imageNamed:]. Find path to the bundle and load them from it with [UIImage imageWithPath].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜