tags:

views:

113

answers:

2

Hi All,

I am developing an game in which i have to load images on finger swipe. For this i loaded all image 22 of size (788x525) at loading of the view itself. Then on finger swipe i just added image on view.

This works fine on iTouch but on iPhone game crashes after showing near about 12 images.

This is how i added images, graphicsArray is NSMUTableArray.

for ( int i=0; i<=totalNoofPages;i++){
    NSString * graphicsFileName = @"Page";
    graphicsFileName =
                [graphicsFileName
                 stringByAppendingString:[NSString stringWithFormat:@"%d", i]];
    UIImage *image =
               [[UIImage alloc] initWithContentsOfFile:
               [[NSBundle mainBundle] pathForResource:graphicsFileName
                                      ofType:@"jpg"]];
    GraphicsImages =
               [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 480,320 )];
    GraphicsImages.image = image;
    [image release];
    [GraphicsImages setCenter:center];

    GraphicsImages.transform = CGAffineTransformMakeRotation(M_PI/2);
    [graphicsArray insertObject:GraphicsImages atIndex:i];
}

and on finger swipe i write,pageNo is number of page that will be displayed.

GraphicsImages = [graphicsArray objectAtIndex:pageNo];
[self addSubview:GraphicsImages];

Is there any way to release the previous loaded image from sub view as it might be kept on stack or is it due to large image size????

Please help me in this situation.

Thanks in advance.

Regards, Vishal.

A: 

Are you crashing due to lack of memory?

You might try pre-loading the previous, current & next images only, and release them as soon as possible. Loading 22 images in memory takes up quite a lot of space.

Ben Scheirman
Hi Ben. Thanks for your reply. I tried that also to load image only when required means on particular page load respective image only. Still result the same. It then crashes on iTouch also.Regards,Vishal.
Vishal Mali
Hi Ben, Thanks for your reply. I just added only one image at a time as i needed. While loading next image i removed the image in following manner [GraphicsImages removeFromSuperview];[GraphicsImages release];So the retain count of the GraphicsImages is 0 and i can add next image. Doing this there is no issue of application slow down after a long play also. Once again thanks for your answer. Regards, Vishal.
Vishal Mali
A: 

Can you provide the error that you're getting when the app crashes? Knowing what exception is being triggered will help a lot in figuring out exactly what's going on.

I can tell you that you aren't releasing the GraphicsImages variable in your for loop, so that's causing memory leaks. I imagine that that's not enough to cause a crash, however. In any case, do this:

[ graphicsArray insertObject:GraphicsImages ];
[ GraphicsImages release ];

Note that you don't need to specify the insertion index in your case because the items will be added sequentially. Also, when you insert an object into an array, it is retained, so you can safely release it afterwards.

LucasTizma
Hi Lucas, Thanks for your reply. I just added only one image at a time as i needed. While loading next image i removed the image in following manner [GraphicsImages removeFromSuperview]; [GraphicsImages release];So the retain count of the GraphicsImages is 0 and i can add next image. Doing this there is no issue of application slow down after a long play also. Once again thanks for your answer.Regards,Vishal.
Vishal Mali