0

I've got a problem while downloading images in my iOS app. My app has the ability to download images form my server, but when I close the app while the download is being done and then I reopen it, for the app the image was fully downloaded, but when I open it it's crashed (half of the image looks OK, but the other half is all grey) and I can't find the way to handle this case.

Is there a way to handle this case? I mean, is there a way to know if the image is crashed, or any other thing that let's me know that the image wasn't downloaded ok for me to redownload it?

FabKremer
  • 2,149
  • 2
  • 17
  • 29

3 Answers3

2

Wrap the image download code in calls to UIApplication beginBackgroundTaskWithExpirationHandler: and UIApplication endBackgroundTask:. In the expiration handler, stop the download and delete the partial file.

This allows the download to keep going for a few minutes in the background giving it more of a chance to complete. If it doesn't complete you delete the partial file preventing the issue of having a bad image. Of course you need to try to download again when your app returns to the foreground.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
1

I would suggest you download an image first to a temporary location and when operation is finished successfully you move the image to it's permanent location (and make changes to database).

Misha Karpenko
  • 2,168
  • 17
  • 18
0

First make sure image is fully downloaded and then save it on device. Do not stream directly on disk.

ninja_iOS
  • 1,183
  • 1
  • 13
  • 20