5

I know the difference between UIImage(named:String) and UIImage(contentsOfFile:String),and I just have over 200MB images saved into Images.xcassets folder. I found that there is a huge memory usage of my app(over 500 MB),so I consider using UIImage(contentsOfFile:String) constructor,because it does not cache image. And I googled a lot and found the following solution.

myImageView.image = UIImage(contentsOfFile: NSBundle.mainBundle().pathForResource("my_img_name",ofType:"png"))

Howerver, when it runs, the app crashed. Here is the link that might relate to my question:Access Asset Catalog pathForResource

Community
  • 1
  • 1
tounaobun
  • 14,570
  • 9
  • 53
  • 75
  • 3
    Are you still referring to the iOS 2 bug that leaked memory when `imageNamed:` was used? That problem was fixed more than 5 years ago. It is safe to use `imageNamed:`. The images are removed from memory when they are not referenced and memory pressure increases. Are you testing on a real device? The iOS Simulator is not really memory constrained, so it does not clear the image cache. – Matthias Bauch Nov 26 '14 at 12:20
  • This, and if I understand the assets concept you won't have separate image files to load as they are going to be "compiled" into a binary with a special structure, even if this is not the case at the current iOs version. – A-Live Nov 26 '14 at 12:30
  • I have to apologize that I did pass a wrong image path to my constructor.Problem solved. – tounaobun Nov 26 '14 at 12:31

0 Answers0