Manage memory with setImageWithURL - ios

I have a bunches of cell that contain UIImageView in UICollectionView and i've using setImageWithURL to load image in each cell, but if it load too much (i.e 100 cell) my app become crashing. Its definitely because of memory issues, i know this is not rare case in Mobile platform, but i still need help from experient guys.
I think it come from setImageWithURL (AFNetworking) problem, it still keep the catch from previous image link and bytheway i've using infinity scrolled, so the system will hold all the images, and memory can not stand. I need a solution for clear the cath images file or some thing to manage the memory. Any help ?

Related

Loaded 50+ images from document directory in UIScrollview app get crash

Loaded 50+ images from document directory in UIScrollview app get the crash.Getting " Connection to assets was interrupted or assets died (with memory warnings)". Can anyone suggest best way to load these images? Thanks in advance. My code is in Objective-C.
The best option for displaying Images in your case would be using a UITableView or UICollectionView because they internally dequeue cells when they are no longer displayed on the screen.
.
In your case the UIScrollView does not dequeue its subviews, thus the screen contains the images all the time which consumes more amount of memory and the operating system closes (crashes) your app due to heavy memory usage.
You should use UITableView instead of UIScrollView.
But if they are very large images, it would crash when just load one image, unless it be preprocessed (resized).

How does the iPhone photo's app load so many images so quickly and smoothly?

I'm building an application that requires a bunch of local images to be displayed in the imageview of a uitableviewcell. However, i'm having difficulty optimizing the performance of the uitableview. I've noticed two issues specifically: first, the view takes a while to load. Second, the scrolling gets laggy when new cells are displayed.
The viewDidLoad is loading in the images like this:
for (Object *object in self.objects)
{
object.thumbnail = [UIImage imageNamed:object.imageName];
}
this is obviously causing the long-load issue, but I'm not sure how else to get those images loaded. Is it a size issue? is this just a bad way of doing it?
The process of displaying the images also seems to be problematic, in other words, even after the images have been assigned to the thumbnail property, they still take too long to be drawn.
Although this is a specific case, I'm curious more generally on how apple loads images in photos so efficiently. Any insights? thanks
Whenever I find that my UI is lagging, the first thing I suspect is that I am performing some operation synchronously (on the main thread) that should be performed asynchronously (on a background thread).
I am also very curious as to how exactly Apple is achieving that performance in the photo app. I am writing an app that has similar requirements as yours right now. My current approach is to load a bunch of photos from disk into memory asynchronously as soon as the user opens my view controller, and continue to load (and remove photos) from memory - ahead of time - as the user scrolls.
Currently, I load each photo from disk asynchronously in cell for row at index path, which is pretty fast, but causes this cascading effect to happen if you scroll quickly through the table view. That is, the cell will appear empty for a moment before the photo appears.
I hope this sheds some light for you.
You may also be interested in trying SDWebImage - which includes an image cache object that has been making my life easier when dealing with local photos.
They are doing everything in the background on various detached threads most likely. This will allow for a great deal of fluidity in applications that are hosting/presenting a great deal of information. I have created a photo gallery myself in various applications, loading many photos simultaneously from different web APIs and whatnot, and by simply creating new threads and managing the allocation efficiently and accurately, you can get a very smooth interface/interaction
Well, the long-load issue is caused by this part of your code - UIImage imageNamed:, because this method caches all the images on the same thread, and also could crash the app if the memory is overloaded.
Try looking at this library - it should do what you are trying to achieve :)
http://www.cocoacontrols.com/platforms/ios/controls/ktphotobrowser

Improving UITableView scrolling

i stuck with some issue of UITableView scrolling. I know that there are numbers of this question and some solutions. However from technical view like (how exactly to do that) i can't find solutions.
Well in my case scrolling is not so smooth because of images in it. It takes lots of time to load it from file. Also i create custom UITableViewCell and using XiB.
These images is using not 1 time but several, the good idea would be to keep them in memory and to load them just once from file. However in my case there could lots of images and app don't have so many memory to keep them. Maybe there are other way to load these images ?
Also images is PNG, maybe i should change type to JPG ?
I would appreciate if someone would give other solutions to improve this scrolling.
Thanks.

Showing large UIImage causes jittering even though it is already in memory

I have app similar to sample app PhotoScroller, e.g. lot of large images (2048x1536) in scrollview. I am not using tile approach as I don't like that partial load effect. I would like to show whole image at once. I am loading images in background thread. When I try to use loaded image in UIImageView for the first time, it blocks main thread for half a second even tho it is already in memory.
I used profiler to see where this lag is coming from but I couldn't find any useful information there.
Is iOS copying image data when it is used for the first time or something like that? Can I somehow do that in background thread as well?
EDIT: when I scroll there and back again and use that same UIImage second time, there is no delay
Try to make image in background thread too.
Do you loading image from inet or locally? bundle or custom path?
I think I solved it. Turns out when ios loads UIImage from disc or web, it doesn't load it in correct format. So when you want to display it, ios has to reformat this image into the format that he can use. This reformating can cause visible stuttering when image is big enough.
To prevent this, you need to reformat UIImage after it was loaded and you can do that safely in background (as far as I know). This link show how to do it.

Image Question for iPad - Memory issues

So I installed my app on my ipad and it crashes due to a memory issue. I figured this was because I'm using really big image files so I went back and reduced them all and essentially cut out about 75% of their size by resizing them and then using PNGCrush.
Now, as for running the program, if I have a background image for each of my 4 individual tabs, would it save memory if I were to set the images to Null every time I switch tabs or should I leave them set? I have one page which has a couple dozen images on it as they act as buttons and from what I'm hearing it sounds like I should clear them all when I'm not viewing that page. Is that correct?
Right now when I boot up, I load all of the images for everything in the app in a sort of: "load it now and be done with it" mentality, though from what I've been reading that causes memory issues as there isn't much memory to use. Does this mean every time I switch tabs or views I want to clear all the images from the ones that aren't visible and then reload them when we go back to them? Would this cause an increase in performance? Or at least prevent crashes? My program works in the simulator but when I run it on my ipad it just explodes =/
Thanks!
EDIT: I'm using Monotouch BTW hence everything is in C#
UIImage BG = UIImage.FromFile("Images/Makes/explosion.png");
UIImage basic = UIImage.FromFile("Images/Models/camaro.png");
UIImage advance = UIImage.FromFile("Images/search.png");
AdvancedSearchButton.SetBackgroundImage(advance, UIControlState.Normal);
ImageSearchButton.SetBackgroundImage(basic, UIControlState.Normal);
MainBG.Image = BG;
BG.Dispose();
basic.Dispose();
advance.Dispose();
Now I know in regular C# dispose() doesn't actually "free" memory, but I read something that says that it gets converted to a "release" when it compiles over to obj-c so that would essentially be freeing those objects.
Also, I'm wondering if I would need to dispose() of the individual buttons and the image after I'm no longer viewing them. I was just setting the image to NULL but that gave me errors.
MainBG.Dispose();
AdvancedSearchButton.Dispose();
ImageSearchButton.Dispose();
Thanks so much for the help!
EDIT2:
So I just tried the above code and the background images and everything else are still there and appears as if nothing is actually getting cleared. Suggestions?
The iPhone does not have virtual memory and it does not have garbage collection. So once something is loaded in the memory it stays in the memory until your code explicitly releases it. If you are not using some resources, you should definitely clean them up as soon as possible.
Also, you should listen to low memory warnings from iOS, which is another opportunity for your code to do some internal clean up.
First off, lazy load your resources unless you have a justifiable reason not to. Secondly, I'm not sure how big your images are, but on large images (ones that are by nature intended for things like backgrounds, or otherwise), generally speaking, I will chop them up into chunks, and load them, again you guessed it, lazily as they're needed.
What you should do is handle your memory warnings properly. Deallocate any resources that are not absolutely critical -- i.e., items on other views in other tabs, or hidden content. You can load them again when you have to.
You should also take a look at using Instruments to find out if you leak (I'm hoping you've already done this), examine your program to see if you can persist some cached resources to disk during low memory situations, etc.

Resources