views:

479

answers:

2

To extend on the subject, ...without crashing? I load about 15 full size images from the photo library in UIImageViews and get out of memory warnings and eventually an app crash. These images range from around 250KB to 400KB. The UIImageViews are set to aspect fit.

The photo library seems to load smaller file size versions of images, rather than just resizing their proportions. Does anyone know how it is able to display so many without crashing because of memory usage?

+2  A: 

The photo library performs many memory optimizations, propably:

  1. loading a thumbnail of the photo, to display as part of the gallery
  2. only loading the thumbnails for the photos on display now (not the ones in next page for example)
  3. only loading the large images once it's actually needed.
  4. ...

To make your life easier, I recommend using the Facebook three20 library, which includes a photo viewer:

TTPhotoViewController emulates Apple's Photos app with all of its flick n' pinch delight. You can supply your own "photo sources", which works similiarly to the data sources used by UITableView. Unlike Apple's Photos app, it isn't limited to photos stored locally. Your photos can be loaded from the network, and long lists of photos can be loaded incrementally.

The project has a nice sample code, so you can start from there. By the way, this is the same view used in the Facebook iPhone app. Here is how it looks like:

alt text

notnoop
It's well documented that three20 used private APIs and a number of people had their app rejected for it.
Was it ever documented what private API's they were, which apps were rejected, which parts of Three20 uses private APIs?
Jasarien
Just search on "three20 private api reject" and you'll get lots of links for more details.
The private API calls in question were apparently left in for debugging purposes. I believe there is a branch of the project that removes them.
Noah Witherspoon
Still, I have to wonder why you would use a library when the supporting developer has exited stage left.
Well, people switch projects constantly, and there is a new maintainers. That's the beauty of actively maintained open-source. It has been reliable so far, minor the false-positive of Apple approval
notnoop
+2  A: 

As you say, you'll want to resize the images. Resizing an image isn't quite as obvious as you'd think, but it's not hard:

// h = new height; w = new width; image = UIImage
CGSize newSize;
newSize.height = h;
newSize.width = w;
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0, 0, w, h)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Of course you'll want to make sure you release your old images so you can reclaim the memory. The best approach is probably to use +imageWithContentsOfFile: since it explicitly doesn't cache.

Rob Napier