Abhinandan Sahgal
Abhinandan Sahgal

Reputation: 1076

iphone memory issue while downloading images

I have a wierd problem. The requirement is to download image from a url on swipe and display it in image view .It is working all fine but I am getting memory warnings after 30 images and after few more swipe app crashes.

Implementation is pretty straight forward,but already spent almost 2 days to figure out the issue. On each swipe i am calling A method :-

-(void)callDownloadImageAPI{

    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    [self loadIndicator:@"Please Wait.!!" :@"Please be patient while we are downloading image for you"];
    @try{
        DownloadImage *downLoadImge =[[DownloadImage alloc] init];
        downLoadImge.delegate=self;
        [downLoadImge getImage:[self.allUrlArray objectAtIndex:self.initialImageViewCounter]];
    }
    @catch (NSException *e) {
        NSLog(@"callDownloadImageAPI exception %@",[e description]);
        [HUD hide:YES];        
    }
    [pool release];
}

This method download 1 image at a time and send UIImage to its delegate

//Implementation of DownloadImage.h and .m

    @protocol DownloadImageDelegate
    @required
    - (void)messageFormServerWithImage:(UIImage*)imageFromSever;
    - (void)gettingImageFailed :(NSString*)errorDesc;
    @end



        @interface DownloadImage : NSObject

        @property(strong) NSURLConnection*                       connection;
        @property(weak) id<DownloadImageDelegate>                delegate;
        @property(strong) NSMutableData*                         data;

        -(void)getImage:(NSString *)imageUrl;

//DownloadImage.m

-(void)getImage:(NSString *)imageUrl{
    @autoreleasepool {

        [[NSURLCache sharedURLCache] removeAllCachedResponses];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
    self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    }
}

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
    @autoreleasepool {

    NSLog(@"connectionDidFinishLoading");
    self.connection=nil;
    if( self.data == nil) {
        return;
    }

//  NSString* jsonStr = [[NSString alloc] initWithData:self.data encoding:NSASCIIStringEncoding];
    UIImage *img=[UIImage imageWithData:self.data];
//  NSArray *messages_json = [parser objectWithString:jsonStr error:nil];
    [self.delegate messageFormServerWithImage:img];
    self.data = nil;
    img= nil;
    }
}

Other delegates of NSUrlConnections are implemented but I am not putting it here . Once this image is returned i am setting this image to scrollview and displaying it and deleting the previous image from scrollview.

More Info:-

Just to verify i commented out setting image to scrollview and just downloaded images on each swipe but still it crashes around 30 images

Surprisingly I am using same class DownloadImage.h and .m to download image at other places in the same work and it work awesome even with 500images .

I am testing in iPod Touch and I checked the memory utilised remain between 12-14mb(never exceed this)

Please help me out guys,let me know if you need more detail.

Upvotes: 1

Views: 205

Answers (1)

shoughton123
shoughton123

Reputation: 4251

Its crashing because all the images are being stored in virtual memory, you need to be caching them and then loading them back into memory when the user actually views them.

Try also setting your image objects to nil after they have been cached or are not needed.

In your class I would also recommend using the didReceiveMemoryWarning method and release some of your images from memory when this is called.

Upvotes: 1

Related Questions