deve1
deve1

Reputation: 195

UIPageViewController with images crash the app

I have used an UIPageViewController i want to display the 24 pages and each page is not a text its an image in contentview controller i m using this code

 self.imgView.image=nil;
 self.imgView.image= [UIImage imageNamed:[NSString  stringWithFormat:@"image%d.jpg",index+1]];

 if(self.categoryId==3 ||self.categoryId==4)
 {
     self.imgView.contentMode=UIViewContentModeScaleToFill;
 }
 else
 {
     self.imgView.contentMode=UIViewContentModeScaleAspectFit;
 }

but the problem is when i run the application and turn the pages after 17 or 18 page it gives me an memory warning and my app get crashed

Upvotes: 1

Views: 302

Answers (1)

sergio
sergio

Reputation: 69027

You could try and use:

NSString* imgFile = [NSString  stringWithFormat:@"image%d",index+1];
NSString* pathToImageFile = [[NSBundle mainBundle] pathForResource:imgFile ofType:@"jpg"];
self.imgView.image = [UIImage imageWithContentsOfFile:pathToImageFile];

Indeed, imageNamed caches all the images you load through it. The cache gets bigger and bigger and it prevents memory from being freed appropriately.

Upvotes: 1

Related Questions