iOS 7中UIImagePickerController拍照或录制视频黑屏的问题

iOS 7 UIImagePickerController has black preview


Answer 1:

There doesn't seem to be a good answer to this anywhere online, so I had to slog through this myself to figure it out.

I'm using ARC (like probably most others these days), so the answer above didn't really help me.

This black camera issue happens as a side effect of doing UIKit stuff off of the main thread on iOS 7. Broadly, background UIKit operations results in all sorts of weird things happening on iOS 7 (memory not being deallocated promptly, weird performance hitches, and the black camera issue).

To prevent this, you need to not do ANY UIKit stuff on background threads.

Things you cannot do on background threads:- Allocate/initialize UIViews and subclasses- Modify UIViews and subclasses (e.g., setting frame, setting .image/.text attributes, etc).

Now, the problem with doing this stuff on the main thread is that it can mess with UI performance, which is why you see all these iOS 6 "background loading" solutions that DO NOT work optimally on iOS 7 (i.e., causing the black camera issue, among other things).

There are two things that I did to fix performance while preventing the ill effects of background UIKit operations:- Pre-create and initialize all UIImages on a background thread. The only thing you should be doing with UIImages on the main thread is setting "imageView.image = preloadedImageObject;"- Re-use views whenever possible. Doing [[UIView alloc] init] on the main thread can still be problematic if you're doing it in a cellForRowAtIndex or in another situation where responsiveness is super important.

Best of luck! You can test how you're doing by monitoring memory usage while your app is running. Background UIKit => rapidly escalating memory usage.

——————————————————————————我是分割线——————————————————————————————


Answer 2:

During one of my recent projects I had this same (or similar) issue, namely the Camera preview showing a black screen when using UIImagePickerController on iOS7 (iPad).

What I discovered is that GCD in itself is not the problem. The problem occurs when you try to use anything at all from the UIKit framework in a thread other than the main thread (as part of an NSOperation).

First of all, let me start by saying that it is clear to me that you shouldn't do this to begin with. In my specific case, however, I had no choice. Since I think that there's a good chance that other people might be experiencing the same weird UIImagePickerController camera behaviour with the same cause, I made up some very simple example code explaining what's going on.

You can find it here: https://github.com/idamediafoundry/CameraTest

The ViewController shows 2 simple buttons. One calls UIImagePickerController, showing the camera, the other starts some GCD operations doing a simple task. If you just open the camera, everything works fine. If you first start the operations and then open the camera, the black preview issue occurs.

Again, you shouldn't call UIKit in anything but the main thread. But I don't think it's normal that breaking this rule causes the camera to malfunction when opening it later in the main thread as part of a completely different flow, right?

Hope this helps.



原链接: http://stackoverflow.com/questions/19081701/ios-7-uiimagepickercontroller-has-black-preview


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值