1

I have a viewController that contains some GIF images,load these images cost a lot of memory ,so it takes me seconds to enter this viewController everytime,is there any way I can enter this viewCOntroller first,then load the data? I don't know NSThread will work or not.My english is poor,hope you can understand my question.Thx.

5 Answers5

2

yes, the easiest way is to use grand central dispatch

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ //enter a background thread
  UIImage * img = [UIImage imageNamed:@"image.jpg"]; //load image in background

  dispatch_sync(dispatch_get_main_queue(), ^{ //return to main thread
      [[self imageView] setImage: img]; //set the imageViews image
  });
});
Fonix
  • 11,447
  • 3
  • 45
  • 74
  • It works,there is no time-delay now :) But there's something wrong with my GIFS,that's another problem,so thank you! – Jiahao Jiang Oct 22 '15 at 07:09
  • Where to implement this? In view did load? – Bhavin Ramani Oct 22 '15 at 09:27
  • @bhavinramani yep, or `viewWillAppear`, would be inefficient to do it in `viewDidAppear` though as the delay in calling that function would just needlessly delay the loading of your images – Fonix Oct 23 '15 at 01:55
1

Just write your code in viewDidAppear: or use a thread in viewDidLoad

dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    // Add your image creation code here e.g.
    UIImage *image = [UIImage imageNamed:@"yourImage.png"];
    dispatch_async( dispatch_get_main_queue(), ^{
        // Add code here to update the UI
        self.imageView = image;
    });
});
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
1

Do the loading of data inside the viewDidAppear method of your ViewController, so the View will appear at first without the GIFS, but as you loop getting each data, you can set the images to your view as they get loaded.

Pietro Pepe
  • 389
  • 2
  • 8
1

Do it like that .

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ 

 // (BackGroungThred)  here you can  retrive the image from your sources . But do not update UI in backgroung thread ..

dispatch_sync(dispatch_get_main_queue(), ^{

// (Main Thread) Update UI in main thread..

});

});

hope it help you ..

Garry
  • 407
  • 2
  • 15
1

There are Two options for load data after entering in a view controller

1.NSOperationQueue

NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
[myQueue addOperationWithBlock:^{
   // Background work
   UIImage * img = [UIImage imageNamed:@"image.jpg"];
  [[NSOperationQueue mainQueue] addOperationWithBlock:^{
    // Main thread work (UI usually)
    yourImageView.image = image;
}];
}];

2.Grand Central Dispatch(GCD)

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
{
          // Background work  
          UIImage * img = [UIImage imageNamed:@"image.jpg"];          
         dispatch_async(dispatch_get_main_queue(), ^(void)
          {
               // Main thread work (UI usually)  
               yourImageView.image = image;           

          });
});

For more details please refer the below links

Link1

Link2

Link3

Link4

Link5

Link6

Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39