Multiple ActivityIndicator for Multiple Images

Discussion in 'iOS Programming' started by Narendar Singh, Jul 13, 2012.

  1. Narendar Singh, Jul 13, 2012
    Last edited: Jul 13, 2012

    Narendar Singh macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #1
    I have a screen with grid 3x3 and I have to download 9 images from 9 different Image URLs.

    Image downloading is Asynchronous.

    For each image I have to show activity indicator. As long as download completes activity indicator should be stopped.

    How can I achieve this?

    See attached image for clear understanding.
     

    Attached Files:

  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    What have you tried? What were the results? What exactly are you stuck on?
     
  3. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #3
    Solved.
    Please check my approach whether I am correct on not.

    1. I have created a class AsyncImage which is responsible for getting image from the server and a delegate AsyncImageDelegate.

    2. I have a ViewController class with Grid Layout which has 9 images (see my screen in 1st post). Every image is a type of AsyncImage. This ViewController is implementing AsyncImageDelegate which has downloadFinished: function.

    3. AsyncImageDelegate is the delegate for all 9 images.

    4. On viewDidLoad I call loadImage: method for each images. Then it starts image downloading asynchronously and also starts 9 Activity Indicator.

    5. As soon as one by one images downloads it called downloadFinished: method, in this method I stop ActivityIndicator based on images ( I m setting tag for each images from 1 to 9).

    If steps are not clear we can have more threads for this discussion.
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    If it does what you want it to, then it's correct, is it not? Without seeing your code, I can't say whether you're leaking or doing anything inefficiently or not.
     
  5. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #5
    make sense, right now app is working fine for me, later on I'll also send you code snippet.

    Thanks
     
  6. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #6

    Your approach sounds good, but as the other poster said, it's hard to tell without seeing the code. Did you write your own async download code? That's really the tricky bit.
     
  7. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #7
    Yes from scratch...
     
  8. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #8
    Shouldn't be that hard, create an custom class with delegate patterns for downloading on a async image call, or just call the urlconnection a different thread?
     
  9. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #9
    true...
     
  10. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #10
    Don't use threads for async downloads!

    Calling NSURLConnection from a different thread is not the way to go. Threads are expensive for the system to set up and run. They tie up physical memory for the lifetime of your app, and make it more likely that the system will kill you to free up memory when another app is frontmost.

    It's worth going to Apple's dev forum and reading posts by an Apple engineer who calls himself Quinn the Eskimo. He has a very long post about the evils of threads that is well worth reading.

    NSURLConnection has full support for async operation built in. That async support uses shared system resources that are fine-tuned for efficiency. Use NSURLConnection and let Apple do the heavy lifting for you.

    Under iOS 5 there are even new block-based NSURLConnection methods that make it easier to handle an async download without having to write a bunch of delegate methods.
     
  11. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #11
    You have written it down perfectly. In our apps we use the async methods for support pre iOS5 and for iOS5 We use the block methods which are really handy.
    I remember that you always took time to write these long, well written posts on iPhoneDevSDK, welcome here.
     

Share This Page