iOS 7 UIImagePickerController 有黑色预览 [英] iOS 7 UIImagePickerController has black preview

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

问题描述

我有一个 UIImagePickerController 被 sourceType 相机调用,并且有 80% 的时间我得到黑色预览.如果我等待,比方说大约 30 秒,我会得到一个很好的预览,它会在大约 50% 的时间里很好,然后它会再次中断.

I have a UIImagePickerController being called with sourceType camera and 80% of the time I get a black preview. If I wait, let's say around 30 seconds, I get a good preview and it will be good for around 50% of the time, then it can break again.

有问题的图像与此非常相似.iDevice 相机显示黑色而不是预览

The image in question is very similar to this. iDevice camera shows black instead of preview

其他人暗示 GCD 可能会导致相机出现一些问题,并且在加载图像选择器时更新 UI 会破坏它.为此,我在每个调用主线程的 GCD 块上加了锁.

Other people imply that GCD might be causing some issues with the camera, and that updates to the UI while the image picker is being loaded break it. I put a lock on every GCD block that calls the main thread for this purpose.

这是一个旋转以模拟活动指示器的图像示例.

This is an example of an image that rotates to simulate an activity indicator.

-(void)animateLoadingImage {

if (isMainThreadBlocked) {
    return;
}

self.radians += M_PI_4 / 2;
[UIView beginAnimations:@"progress rotation" context:nil];
[UIView setAnimationDuration:.1];
self.loadingImageView.transform = CGAffineTransformMakeRotation(self.radians);
[UIView commitAnimations];


}

PS:对尚未渲染的视图进行快照会生成空快照.确保您的视图在屏幕更新前或屏幕更新后至少呈现一次.

PS: Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

当我尝试打开选择器控制器时,它始终显示,但即​​使相机正确显示预览,它也会显示它.我不认为错误就在这里,但这也让我很烦恼.

This shows ALWAYS when I try to open the picker controller, but it shows it even if the camera shows the preview correctly. I don't think the error is around here but this bugs me a lot also.

推荐答案

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

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.

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

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