我需要加快UIImagePickerController
的速度。我的应用程序将从可能的多个控制器中调用UIImagePickerController
,并且在每个控制器中有两个按钮,一个用于照片库,另一个用于相机。这是我的示例应用。我尝试的一些解决方案是Apple在“并发编程指南”中推荐的。
最简单的是分配&用户按下按钮时初始化实例。这可能需要2秒才能显示相机。
解决方案尝试: - (1)我制作了一个@property(...)* imagePicker并调用了它的alloc&在viewDidAppear
初始化以使其看起来很平滑,但它干扰了其他元素的加载,可能是因为它使用了相同的线程。在initWithNibName
或viewDidLoad
中使用时会出现更糟糕的结果。
(2)Operation queues
...不令人满意的结果。
(3)Dispatch Queues
......效果更好,但仍有明显的波动表现。
(4)performSelectorInBackground:withObject:
效果不佳。除非另有建议,否则我认为我不想走向全球。我在制作UIImagePickerController
的两个@properties方面没有问题。我将继续“线程编程指南”。
如果可以,请在您的解决方案中包含所有必要的内存管理实践。谢谢。
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// (1) TRYING OPERATION QUEUES
// (a) --- NSBlockOperation
NSBlockOperation *NSBO_IP = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"before");
imagePicker = [[UIImagePickerController alloc] init];
// 1.9, 1.6, 1.4 seconds pass between 'before' and 'after'
// it seems this method has a dynamic technique, executes in different order every time
NSLog(@"after");
}];
// (b) --- NSInvocationOperation
NSInvocationOperation *NSIO_IP = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadTheImagePicker) object:nil];
// --- Add an operation
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//NSLog(@"time 1");
// (a) [queue addOperation:NSBO_IP];
// (b) [queue addOperation:NSIO_IP];
//NSLog(@"time 2");
// (2)TRYING DISPATCH QUEUES
// (a) --- GCD call (do I need to release 'myQueue' at some point since this is C-level call?)
/*
NSLog(@"time 1");
dispatch_queue_t myQueue = dispatch_queue_create("My Queue", NULL);
dispatch_async(myQueue, ^{
imagePicker = [[UIImagePickerController alloc] init];
// 1.2, 1.2, 1.2, 1.4 seconds significant increase over Operation Queues
// a solid constant executing technique, executes very consistently
});
NSLog(@"time 2");
*/
// (3)TRYING performSelectorInBackground:withObject (not recommended?)
NSLog(@"time 1");
[self performSelectorInBackground:@selector(loadTheImagePicker) withObject:nil];
// 1.3, 1.7, 1.3 seconds pass between 'before' and 'after'
NSLog(@"time 2");
// RESULTS REFLECTED IN LINE BELOW !
[self changeText:self]; // text does not change on time when trying to alloc & init an ImagePickerController
}
- (void)loadTheImagePicker
{
NSLog(@"before");
imagePicker = [[UIImagePickerController alloc] init];
// --- NSInvocationOperation used
// 1.6, 1.2, 1.4 seconds pass between 'before' and 'after'
// this method has a more constant executing technique, as far as order of executions
// --- NSInvocationOperation used
NSLog(@"after");
}
答案 0 :(得分:3)
from @Murat's answer in a comment:
I found the solution. This question has already been answered. It seems when connected to Xcode debugger the
[UIImagePickerController alloc] init]
takes much longer to execute than when running the App without Xcode.My solution is still to keep
@property
and do thealloc
&init
in theviewDidLoad
method.The other solution is here: UIViewController - mysteriously slow to load
moved this to an answer as I almost missed it as a comment.