eoLithic
eoLithic

Reputation: 883

App crashes when trying to display three panoramic photos on one screen

So, it's a fairly simple question. I have a screen which has a button. Every time a user taps the button we let the user to pick a panoramic photo from his Photos, and add a UIImageView that displays the panoramic photo. Each UIImageView has a frame ~375x150 points(to show panoramic photo completely on iPhone 6).
The problem is that when we add second UIImageView we receive an error in NSLog: "Connection to assetsd was interrupted or assetsd died". Plus, we get slight UI 'freezes'. But UIImageView displays the second photo and we're absolutely okay.
And when we add third UIImageView the app simply crashes and Xcode displays "Lost connection to "Admin's iPhone 6"."
Considering panoramic photo resolutions (~13600x~3000) it's completely understandable why the app crashes and has UI freezes. The question is: is there anyway (except simply compressing the photo and showing the resized version on the screen) to show a lot of panoramic photos without lags? Looking at the Photos app, it displays panoramic photos without any bugs and doesn't have any UI freezes.

Upvotes: 2

Views: 722

Answers (1)

dandan78
dandan78

Reputation: 13854

Your question is desceptively simple. You are right, the size of your images means that your app is grappling with memory issues. You're passing a huge image to a UIImageView and hoping it will take care of everything for you. It won't, not with that much data.

The way the built-in Photos app gets around this is by using a technique called tiling, combined with resizing. It works something like this:

  1. Initially, it displays a scaled-down version of the panarama.
  2. When you begin to zoom in, it gets the coordinates of your fingers, calculates where they are relative to the full-sized image.
  3. Then it calculates what parts its needs to grab from the full-size image, gets that at full resolusion, then scales and displays just that part. Continued zooming consequently takes a mere fraction of the memory that would be needed for the full-size image.
  4. At the same time, it calculates rectangles of screen size for the surrounding area and keeps them ready for when you decide to pan, but obviously not at full resolution.
  5. As you continue to scroll/zoom, the process is repeated.

Google around, you might be able to find a library that takes care of that for you, and I think there even might have been an example from Apple (see this question). Otherwise, it's an interesting learning experience to write it yourself.

Upvotes: 5

Related Questions