iphone: a way to optimize image loading on scrollview. - iphone

In my app, I display images with scrollview. I avoid memory issue by adding view as user scroll through. Scrolling seems fine but it stop in split second when new image is loaded on next/pervious view. I tried to load image on NSThread but no performance was improved.
I use [UIImage imageWithData:] to load image. I wonder if there is a way to load an image more efficient way. It would be appreciated for any comments regard this issue.

I think the best way is to keep three pictures at once in memory: the picture on the current screen, the picture on the next screen and the picture on the previous screen. By doing this you can scroll smoothly. You can use your imageWithData method with this procedure.
It gets even simpler if you use imageNamed instead, because this method caches the pictures if it has enough memory. So you just have to load the current picture and the next picture by just calling
[UIImage imageNamed:#"nextPic"];
By executing this, it loads the picture into the cache but does not display it. The next time you have to display the picture it is already in memory.

Related

How to load images with high Res in UIscrollView

Can anybody tell me how to load or set images with high resolutions in UIScrollVIew .
I am getting memory warning and app is getting crashed without any stack trace.
Thank you in advanced.
I was also working on an app which had UIScrollView and had to request for High Resolution Images. The thing is, you should first check the size of the images being downloaded. Lets say, if the image size is more than 10MB, then it would be difficult for your app to survive with such a heavy load.
You can consider the following methodologies.
1.Download your images and compress them.
2.At a particular instance of time, there is no need to populate your Scroll Views array with all the images at once, instead try to have only three images,i.e
1.The current one which is being shown.
2.The previous one.
3.and the image next to the current one.
You can also take help from the link
http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content
You should use the concept of lazy loading. As you said you used UIScrollView that's mean you have lot's of images and I guess you show them one by one or two or three at a time. Load only that images which are visible to the user. One more thing you have to maintain that is when user swipe to next image or next branch of images that's mean next image(s) that's are going to visible to the user. At that time before load the new images release all the previously loaded images from memory. I hope it'll solve your problem.
If you are trying to create multiple UIImageView of high resolution image inside UIScrollView, then it will create memory warning and application will crash.
I will suggest you to try reusable UIImageView , like create 3 UIImageView and replace images in it.
Try circular scrollview.link or UICollectionView

How to modify an image before caching it with HJCache?

I've been using HJCache to do asynch image loading and caching. It works great, but I have use case where I can't seem to get HJCache to handle.
I have an images loaded in a table view, and the same images appears bigger when you select the corresponding tableviewcell. HJCache does a great job of caching the image so it doesn't have to reload it after getting it once. However, I would like to resize the image (and do some cropping, etc) in the tableview thumbnail. The problem is, that it's a very expensive task to do and it ends up lagging the scrolling of the tableview if it's done in the drawRect of the cell.
I would like to cache the "modified" image along with the original so that the image processing only has to happen once. How do i get an instance of the already cached UIImage, apply the processing, and then add that one to the cache as well (with a different oid). I only seem to be able to cache an image by giving a URL.
Thank you,
Matt

iphone best practice, how to load multiple high quality images

I have about 20-ish high quality images (~3840x5800 px) that I need to load in a simple gallery type app. The user clicks a button and the next image is loaded into the UIImageView.
I currently use [UIImage imageWithContentsOfFile:] which takes about 6 seconds to load each image in the simulator :(
if I use [UIImage imageNamed:] it takes even longer to load but caches the images which means its quicker if the user wishes to see the same images again. But it may cause memory problems later with all that caching crashing my app.
I want to know whats the best practice for loading these? I'm experimenting with reducing image file size as much as is possible but I really need them to be high quality image for the purpose of the app (zoomable, etc.).
Thanks for any advice
[EDIT]
Hey again guys,
Thanks for all ye're advice. The project's spec's have changed a little. Now as well as displaying the images they firstly have to be zoomed in to a particular spot and when the user taps next it zooms out and then displays the next image. So I'm not sure if the proposed solutions fits?
Apple's docs recommend against trying to load single images that are larger than 1024x1024. You should look into using CATiledLayer instead, to load pieces of the images as needed.
You can have a look at this Apple sample:
http://developer.apple.com/library/ios/#samplecode/PhotoScroller/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010080
It shows how to load big images, breaking them in tiles for different zoom levels.
You can't see all those pixels at any given time, so there is no need to load them all. Load lower-res copies ("big-thumbnails") to view the complete image, then selected sub-tiles, maybe of 2 or more different resolution sets, after the user zooms in.
The CATiledLayer API may be able to handle some of the latter for you.

How to get thumbnail from a live camera or a NSURL

I want to know that Can we get thumbnails from a live camera or video? If yes then how? Please suggest me what is best way to show more then 20 or 30 live cameras in a UITableView. I have customized the UITableViewCell and added a UIWebView to that cell with the loadRequest. But this is much slower and also got unvisitable on table scroll. How I can get out of these problems.
Thanks,
maybe you can get the data stream from the camera, find how to make an image with that stream and then create an NSOperation to load asynchronously each image view in every visible cell.
To make a table view slick you need to use dequeueing of table view cells. You should also keep track of which cells are being displayed, which ones are going to be (when scrolled) and which ones won't be visible any more, to manage the loading/unloading of the thumbnails.
Bare in mind that you should get the original stream, scale it, then store it. Otherwise you will waste memory storing a big picture that is being downscaled to be shown as a thumbnail.
I hope this brings up some ideas. best of luck

Is it possible to know if an image is in the iPhone system cache?

I am trying to improve scrolling performance of a UITableView. I am loading the images using imageNamed: and dispatch_async, but scrolling is very good once the images have been loaded into the cells. I would like to fade in the images only if they are not in the system cache to reduce the jarring effect of the images "popping" into view.
Is there a way to know if an image is already in the system cache?
There is no documented way to look inside the UIImage to check such things.
I think the only way to know for sure that the image is available immediately, is to force the UIImage to be loaded. This can be done in the background, by creating the UIImage and accessing it's pixels, using CGImage functions. If you ensure that there is no rescaling needed (i.e. don't put a 3000x2000 image in a 30x20 space) then it should display without a glitch.