iOS、ARC:后台线程冻结 UI

发布于 2024-12-14 03:20:48 字数 940 浏览 0 评论 0原文

我有一个 UIViewControllerUITableView 作为子视图。当单击某个单元格时,应该显示一个UIImagePickerController。由于初始化时间较长,因此当 UIViewController 出现时,我在后台执行此过程。
现在我将 ARC 添加到我的项目中,然后它仍然无法工作。 UI 在初始过程中被冻结。

这是我的代码。

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self performSelectorInBackground:@selector(initImagePickerControllerInBackground) withObject:nil];
}

...

- (void)initImagePickerController
{
    if (imagePickerController != nil)
        return;

    imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.allowsEditing = YES;
    imagePickerController.delegate = self;
    imagePickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
}

- (void)initImagePickerControllerInBackground
{
    @autoreleasepool
    {
        [self initImagePickerController];
    }
}

我应该改变什么才能让它为我工作?

I have a UIViewController with a UITableView as subview. When a certain cell was clicked an UIImagePickerController should be presented. Because of the long initialization time it takes, I perfom this process in the background when the UIViewController did appear.
Now I added ARC to my project and then it didn't still work. The UI was freezed by the initial process.

Here's my code.

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self performSelectorInBackground:@selector(initImagePickerControllerInBackground) withObject:nil];
}

...

- (void)initImagePickerController
{
    if (imagePickerController != nil)
        return;

    imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.allowsEditing = YES;
    imagePickerController.delegate = self;
    imagePickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
}

- (void)initImagePickerControllerInBackground
{
    @autoreleasepool
    {
        [self initImagePickerController];
    }
}

What should I change to get it working for me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅的被狗咬 2024-12-21 03:20:48

UIKit 不是线程安全类,不应从主线程外部调用。您不应该在后台执行此 UIImagePickerController 分配/交互。

UIKit is not a thread safe class and should not be called from outside the main thread. You should not perform this UIImagePickerController allocation/interaction in the background.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文