I have a class that generates images to be printed by the user. These images are created using QuartzCore (and some UIKit elements) and need to be run on the main thread.
我有一个类,用于生成要由用户打印的图像。这些图像是使用QuartzCore(和一些UIKit元素)创建的,需要在主线程上运行。
In the view visible to the user while the images are being generated, I have a progress bar. This view is the delegate of the class that does the printing, and a method is called by the printer on the view to update the progress bar. The problem is, the progress bar doesn't visibly update until the printer is finished because the printer is clogging up the main thread. I can't move the progress bar off of the main thread because all UI updates must be done on the main thread.
在生成图像时用户可见的视图中,我有一个进度条。此视图是执行打印的类的委托,并且视图上的打印机调用方法以更新进度条。问题是,进度条在打印机完成之前不会明显更新,因为打印机堵塞了主线程。我不能将进度条从主线程移开,因为所有UI更新必须在主线程上完成。
I'm fairly new to multithreading; do I have any other options or must I go with an activity indicator or something similar?
我对多线程很新;我有其他选择,还是必须使用活动指示器或类似的东西?
2 个解决方案
#1
1
Update your code so that the image creation is done on a background thread. This should be safe.
更新代码,以便在后台线程上完成映像创建。这应该是安全的。
Then you can make calls onto the main thread to update the progress bar.
然后,您可以调用主线程来更新进度条。
#2
0
You can use GCD, Raywenderlich Tutorial
您可以使用GCD,Raywenderlich教程
- (void)generatePage
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
/*
Here you can generate page and update progress
For example:
*/
[self updateProgress:10.0f];
sleep(1000);
[self updateProgress:20.0f];
sleep(3000);
[self updateProgress:100.0f];
});
}
- (void)updateProgress:(float)progress
{
dispatch_async(dispatch_get_main_queue(), ^{
progressView.progress = progress;
});
}
#1
1
Update your code so that the image creation is done on a background thread. This should be safe.
更新代码,以便在后台线程上完成映像创建。这应该是安全的。
Then you can make calls onto the main thread to update the progress bar.
然后,您可以调用主线程来更新进度条。
#2
0
You can use GCD, Raywenderlich Tutorial
您可以使用GCD,Raywenderlich教程
- (void)generatePage
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
/*
Here you can generate page and update progress
For example:
*/
[self updateProgress:10.0f];
sleep(1000);
[self updateProgress:20.0f];
sleep(3000);
[self updateProgress:100.0f];
});
}
- (void)updateProgress:(float)progress
{
dispatch_async(dispatch_get_main_queue(), ^{
progressView.progress = progress;
});
}