gpt4 book ai didi

ios - 加载 UIImagePickerController 更快?

转载 作者:行者123 更新时间:2023-11-28 20:09:47 29 4
gpt4 key购买 nike

我需要更快地加载 UIImagePickerController。我的应用程序将从可能的多个 Controller 调用 UIImagePickerController,并且在每个 Controller 中有两个按钮,一个用于照片库,另一个用于相机。这是我的示例应用程序。 Apple 在“Concurrency Programming Guide”中推荐了我尝试过的一些解决方案。
最简单的是在用户按下按钮时分配并初始化一个实例。这最多可能需要 2 秒钟,然后相机才会出现。
解决方案尝试:-
(1) 我做了一个@property (...) *imagePicker 并在viewDidAppear 中调用了它的alloc & init 让它看起来很流畅,但是它干扰了其他元素的加载,可能是因为它使用了相同的线程。在 initWithNibNameviewDidLoad 中使用时会出现更差的结果。
(2) 操作队列...结果不理想。
(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");
}

最佳答案

来自@Murat 在评论中的回答:

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 the alloc & init in the viewDidLoad method.

The other solution is here: UIViewController - mysteriously slow to load

将此移至答案,因为我差点错过它作为评论。

关于ios - 加载 UIImagePickerController 更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20753151/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com