Jiahao Jiang
Jiahao Jiang

Reputation: 33

how to load data after entering a viewController?

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.

Upvotes: 1

Views: 296

Answers (5)

user3182143
user3182143

Reputation: 9609

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

Upvotes: 1

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39998

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;
    });
});

Upvotes: 1

Garry
Garry

Reputation: 407

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 ..

Upvotes: 1

Pietro Pepe
Pietro Pepe

Reputation: 389

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.

Upvotes: 1

Fonix
Fonix

Reputation: 11597

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
  });
});

Upvotes: 2

Related Questions