Reputation: 332
I'm making an iOS app in which, if memory were not an issue, there would be thousands of images loaded on the screen simultaneously. Obviously, this is not a good approach, so what I want to do is "unrender" some of these images so that only a few are on screen at any given point, and then "rerender" them when the user takes certain actions. So my question is, how could I do this?
Or, if it's any easier, all of the images are the same; I know this may sound kind of weird, but yes my app could require thousands of the same image to be loaded on screen. Since this is the case, is memory management not really an issue since they'll all be referencing the same file? (and by not an issue, I mean such that I can just use the hidden
property to change what is and is not on screen). If I can do this, I was doing
UIImage *image = [[UIImage alloc] initWithContentsOfFile:@"image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[scrollView addSubview:imageView];
(scrollView is a UIScrollView
that I was using to give pinch to zoom functionality to the UIImageView
). The issue, though, is that when I do this the UIImageView
isn't rendered at all.
Any other ideas or suggestions are of course appreciated, and thanks in advance :D
Upvotes: 0
Views: 1527
Reputation: 437402
The answer depends upon your UI. When you say thousands of images, how many are visible at one time. Is it kind of like a huge photo gallery where only a few are actually visible at any given time? In that case, you'd probably just maintain views for just the images visible at the time, and the ones that are scrolled off the visible screen will not have any views associated with them until they scroll on (and those that are were visible but are now scrolled off will be released).
But if you're dealing with a different problem, tons of little images, many of which may be visible at the same time, that's a different problem (addressed with links like that provided by qegal).
I assume you're trying to do something more like the latter, but regardless, you have to give us a slightly better idea of the nature of the images and what sort of user experience we're dealing with here.
Update:
Sounds like you're doing something more like the former. In that case...
Check out PhotoScroller, Apple's example of navigation through a large collection of images (only three images are in the app, but you get the idea).
Personally, I have a low tech implementation, where I have a photo browser where I'm panning horizontally (UIPanGestureRecognizer) through a large collection of images (roughly 100, but could just as easily be many, many more). My view controller, though, only has three UIImageView controls, one for the main image being shown, one for the one to the previous image (and it's frame is set just off screen to the left), and one for the next image (and it's frame is set just off screen to the right). As I receive continuous feedback from the UIPanGestureRecognizer, I adjust all three frames by the horizontal portion of the translationInView of the gesture recognizer (I've seen other implementations that use a scrollview to coordinate the movements of the images, but the idea is still the same). When I let go, my gesture recognizer figures out whether I successfully swiped to the next or previous image, completes that animation, and when it's done, it relocates the three UIImageViews (previous offscreen to the left, current on screen, and next offscreen to the right) and reloads the three image views with the appropriate "current", "previous", and "next" images. At that point you're ready for another swipe with your gesture recognizer.
Upvotes: 1