0

I've created a small photo gallery which is presenting a new view controller with a larger version of the photo and some additional text when it is clicked:

enter image description here

The problem is - after going through a handful of images - the application crashes due to overuse of memory. I attempted to resolve this by compressing the images in order to leave a smaller memory footprint, but the issue remains and I'm not sure what else I can do to resolve this issue.

enter image description here

Also - there is almost no code to doing this since I'm using storyboard's push segues as well as the built in navigation item to go back between viewControllers.

enter image description here

P.S.

If you feel source code is necessary to provide insight in this instance - it can be found here:

https://www.dropbox.com/s/q1qq8pq4tzv8wyo/EXAMPLE%20BUILD.zip?dl=0

2 Answers2

0

To resolve this issue you have to use this trick; Put a "placeHolder" image in your cell's imageview in "StoryBoard". Don't load the images all at once in your "ViewController", load them one by one by running a loop or in your "cellForRowAtIndexPath()" method and add a delay in each iteration (Load first image then add a delay, load second image and add a delay, then for third one and so on up to the last image).

If you want to know how to add a delay then check this link:

NSTimer - how to delay in Swift

Community
  • 1
  • 1
Zohaib Hassan
  • 984
  • 2
  • 7
  • 11
  • Even if the crash occurs not when loading all the thumbnails but after they all load then loading the non thumbnail view and going back and forth between the two views 3 or 4 times? – AndroidAndroidAndroid May 19 '17 at 21:53
  • Maybe your images are being load again or again, or maybe the issue is that you are passing a large image view object again and again, every time in your segue. If you are passing the imageview object then I'll suggest you to pass the path/url of image and load the image from that path/url in "SecondViewController". – Zohaib Hassan May 19 '17 at 21:59
  • I simply added the image for each object using Storyboard – AndroidAndroidAndroid May 19 '17 at 22:09
  • and yes - when toggling back and forth the images are loaded multiple times - which is when the crash occurs – AndroidAndroidAndroid May 19 '17 at 22:10
  • Then make sure you are not calling "yourCollectionView.reloadData()" method in "viewWillAppear()" or "viewDidAppear()" method. It this is the case then remove it. – Zohaib Hassan May 20 '17 at 11:57
0

To resolve this issue I simply resized the images - I noticed I accidentally used a gigantic (6000 x 4000) image and even though I compressed the images iOS had to crunch pretty hard to resize them into the view... thereby causing the memory leak and subsequent crash.

Resizing them to 600x400 did the trick.