且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS 7 UIImagePickerController 有黑色预览

更新时间:2022-12-23 13:51:05

在网上的任何地方似乎都没有一个好的答案,所以我不得不自己费力地解决这个问题.

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.

我正在使用 ARC(可能像现在大多数其他人一样),所以上面的答案并没有真正帮助我.

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

这个黑色相机问题是在 iOS 7 上从主线程执行 UIKit 操作的副作用.总的来说,后台 UIKit 操作会导致在 iOS 7 上发生各种奇怪的事情(内存没有被及时释放,奇怪的性能故障,以及黑色相机问题).

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).

为了防止这种情况,你不需要在后台线程上做任何 UIKit 的事情.

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

你不能在后台线程上做的事情:- 分配/初始化 UIViews 和子类- 修改 UIViews 和子类(例如,设置框架,设置 .image/.text 属性等).

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).

现在,在主线程上做这些事情的问题是它会干扰 UI 性能,这就是为什么你会看到所有这些 iOS 6后台加载"解决方案在 iOS 7 上不能以***方式工作(即导致黑色相机问题等).

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).

我做了两件事来修复性能,同时防止后台 UIKit 操作的不良影响:- 在后台线程上预创建和初始化所有 UIImages.你应该在主线程上对 UIImages 做的唯一一件事是设置imageView.image = preloadedImageObject;"- 尽可能重复使用视图.如果您是在 cellForRowAtIndex 中或在响应性非常重要的其他情况下在主线程上执行 [[UIView alloc] init] ,那么它仍然可能会出现问题.

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.

祝你好运!您可以通过在您的应用程序运行时监控内存使用情况来测试您的工作情况.后台 UIKit => 快速增加内存使用量.

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.