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

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

问题描述

我有一个使用sourceType相机调用的UIImagePickerController,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.

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

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天全站免登陆