Reputation: 4160
In my iOS application i have an UIScrollView in which there are many UIImageViews with UIImage. When the user scrolls down, other images are downloaded from the web and they're added to the scrollview. My problem is that when there are too many images (more than 50), the scrollview become slow but i don't receive any memory warning. How can i solve this? I hope i explained myself.
Upvotes: 2
Views: 1861
Reputation: 1045
I know this thread is old, but whoever encounters this problem take a look at 3rd party library for reusable scroll view. It uses the same principles as table view. You can load small images and replace them with actual size images only when shown. You can load thousands of images with no memory impact. It is the way apple uses in the native photo app. The trick is you don't load all of the images at once but few at the time. It is really great to improve memory management of your app, with no impact on user experience
Here is a link: https://github.com/sumofighter666/ReusableScrollView
Upvotes: 0
Reputation: 11409
You can downscale the images if they are using too many resources:
// compressionFactor is a factor between 0.0 (worst quality) and 1.0 (best quality)
NSData *imageData = UIImageJPEGRepresentation(image, compressionFactor);
UIImage *newImage = [[UIImage alloc] initWithData:imageData];
EDIT: But a far better solution would be to use an image caching system such as SDWebImage. It is extremely easy to use and lets you use a placeholder loading image. It also loads images asynchronously so your UI isn't blocked and it will release images from the cache before your application is overloaded. You simply load images like this:
[some_uiimageview setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"LOADING_IMAGE.png"]];
// where imageUrl is the web address you're loading the image from
Upvotes: 0
Reputation: 31339
Here is the answer: uiscrollview-and-lazy-loading
If you have encountered memory problems loading too many images into your UIScrollView, lazy loading them is your answer.
Lazy loading describes an easy technique to load only what should be shown, no more.
Lazy loading of images in a UIScrollView is critical because of the iPhone/iPads/iPods low physical memory.
Doing it on the other hand is very easy if you follow these four steps:
Listen for scrollViewDidScroll on your delegate, like this.
Calculate the current page using the known size of your images.
Look if the image already exists, if not, add it.
And don’t forget to clean your memory.
Final combination of all steps:
-(void)scrollViewDidScroll:(UIScrollView *)myScrollView {
/**
* calculate the current page that is shown
* you can also use myScrollview.frame.size.height if your image is the exact size of your scrollview
*/
int currentPage = (myScrollView.contentOffset.y / currentImageSize.height);
// display the image and maybe +/-1 for a smoother scrolling
// but be sure to check if the image already exists, you can do this very easily using tags
if ( [myScrollView viewWithTag:(currentPage +1)] ) {
return;
}
else {
// view is missing, create it and set its tag to currentPage+1
}
/**
* using your paging numbers as tag, you can also clean the UIScrollView
* from no longer needed views to get your memory back
* remove all image views except -1 and +1 of the currently drawn page
*/
for ( int i = 0; i < currentPages; i++ ) {
if ( (i < (currentPage-1) || i > (currentPage+1)) && [myScrollView viewWithTag:(i+1)] ) {
[[myScrollView viewWithTag:(i+1)] removeFromSuperview];
}
}
}
Upvotes: 3
Reputation: 14302
Use Apple's Image I/O framework.
With Image IO, you can:
If you want to get up to speed quickly, Bill Dudney has a great iBook on the topic
https://itunes.apple.com/au/book/all-image-io-you-need-to-know/id601759073?mt=11
The book has other useful tidbits on:
Upvotes: 0