iOS 7 UIImagePicker预览黑屏 [英] iOS 7 UIImagePicker preview black screen

查看:441
本文介绍了iOS 7 UIImagePicker预览黑屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从我的代码加载相机时,相机预览为黑色。如果我等待10-20秒,它将显示真实的相机预览。我发现了几个问题,其中一些问题表明在后台运行其他代码应该是这个问题的原因。但是我没有在后台运行任何代码。
我该如何解决这个问题?

When i try to load camera from my code, camera preview is black. If I wait for 10-20 seconds it will show real camera preview. I found several questions and some of them suggest that running some other code in background should be the reason for this. However I don't have any code running in background. How should I fix this?

这是我运行相机的代码

UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init];

photoPicker.delegate = self;
photoPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:photoPicker animated:YES completion:NULL];


推荐答案

大约5个月前,我的团队发现内存泄漏iOS7中的UIImageViewController。每个实例化都以指数方式减慢app的速度(即第一个alloc-init延迟1秒,第二个延迟2秒,第三个延迟5秒)。最终,我们有30-60个延迟(类似于你遇到的情况)。

About 5 months ago my team discovered a memory leak with UIImageViewController in iOS7. Each instantiation slowed down the app exponentially (i.e. first alloc-init had a 1 second delay, second had a 2 second delay, third had a 5 second delay). Eventually, we were having 30-60 delays (similar to what you're experiencing).

我们通过继承UIImagePickerController并使其成为Singleton解决了这个问题。这样它只被初始化一次。现在我们的延迟很小,我们避免泄漏。如果子类不是一个选项,请在viewController中尝试一个类属性,只是懒得加载它。

We resolved the issue by subclassing UIImagePickerController and making it a Singleton. That way it was only ever initialized once. Now our delay is minimal and we avoid the leak. If subclassing isn't an option, try a class property in your viewController and just lazy load it like so.

-(UIImagePickerController *)imagePicker{
    if(!_imagePicker){
        _imagePicker = [[UIImagePickerController alloc]init];
        _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    return _imagePicker;
}

然后您可以稍后再调用它:

Then you can just call it later like:

[self presentViewController:self.imagePicker animated:YES completion:nil];

这篇关于iOS 7 UIImagePicker预览黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆