开发者

UIImage cache clearing problem

My image was loading from data server consumes a lot of memory, if images increase and once i moved to previous screen the used image memory is still consuming even i not using "alloc" . And is it possible to clear cache if once move away from the screen. is there any best way to avoid it?

for (unsigned i = 0 ; i<displayedImages.count; i++) 
    {
        if (![operation isCancelled]) 
        {
            NSLog(@"loadImage %d",i);
            NSString *strURL = [[[imageList objectAtIndex:i] objectForKey:@"URL"] stringByAddingPercentEscapesUsingEncoding:4];
            NSURL *url = [NSURL URLWithString:strURL];
            NSData *imageData = [NSData dataWithContentsOfURL:url];// [[NSData alloc] initWithContentsOfURL:url];
            UIImage *image = [UIImage imageWithData:imageData];// [[UIImage alloc] initWithData:imageData];

            indexForSetImage = i;
            [self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:NO];
     开发者_如何学Go   }
    }

Any suggestion is greatly appreciated.

Regards,

Sathish


You need to use Lazy Loading code as a solutions for the problem occured with you.

In this example they are using Table view , but instead of using the tableview you should use the touch event or any button for next image, when that button is pressed then the image shall be downloaded and then will be shown.

And yes If you will try to load all the images using for loop , it will take a lot time as well as it may get consume a lot memory and may result in the crash of the application.

Hope this will help you.


Lazy loading is required and also the way to manage memory is to make sure that you don't hold resource to something that you don't need.

So once an image is not in view you must not still hold a ref to it. So for example of there is an UIImageView *temp, and you are not displaying it any more you must do this:

temp.image = nil;

This ensures that you don't hold ref to it. Also if you need more help regarding this have a look at this. It also has some caching built into it so that you don't have to bother about it

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜