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.
Asked
Active
Viewed 296 times
1
-
`viewDidAppear` not `viewDidLoad` – Ty Lertwichaiworawit Oct 22 '15 at 06:38
5 Answers
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
-
-
@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
-
I use this method and change the priority to DISPATCH_QUEUE_PRIORITY_BACKGROUND,so it works well,thank you! Can I accept two different answers? – Jiahao Jiang Oct 22 '15 at 07:15
-
No you can't accept two answer but surely you can upvote two answers – Inder Kumar Rathore Oct 22 '15 at 07:18
-
-
-
-
sorry,acturely I have 18 now,I checked the wrong place. Now I can upvote your answers :) – Jiahao Jiang Oct 22 '15 at 07:44
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

Community
- 1
- 1

user3182143
- 9,459
- 3
- 32
- 39
-
-
if my answer is very understandable and helpful for you,please tick my answer. – user3182143 Oct 22 '15 at 09:59