9

我有一个使用 UIImagePickerController 允许用户拍照的应用程序。

我已将测试用例简化为单个视图控制器应用程序中的最简单序列。这是我的代码。

//
//  CTViewController.h
//  Camera Test
//

#import <UIKit/UIKit.h>

@interface CTViewController : UIViewController <UINavigationControllerDelegate,   UIImagePickerControllerDelegate>

@property (nonatomic, retain) UIImagePickerController *cameraController;

- (IBAction)takePicture:(id)sender;

@end

代码主体如下:

//
//  CTViewController.m
//  Camera Test

#import "CTViewController.h"

....

- (void)didReceiveMemoryWarning
{
    NSLog(@"%s", __func__);

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)takePicture:(id)sender
{
    self.cameraController = [[UIImagePickerController alloc] init];

    self.cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.cameraController.allowsEditing = YES;
    self.cameraController.delegate = self;
    [self presentViewController:self.cameraController animated:YES completion:nil];
}

“takePicture”连接到我可以在屏幕中间按下的按钮。

在 ios 6 上一切正常,但在 ios 7 上,一旦出现视图控制器,我就会收到一连串的内存警告。因此:

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.
Received memory warning.
-[CTViewController didReceiveMemoryWarning]

.... <here I take a picture, which I do nothing with>

Received memory warning.
-[CTViewController didReceiveMemoryWarning]
Received memory warning.
-[CTViewController didReceiveMemoryWarning]

.... <I get a cascade of these warnings>

该应用程序是使用 xcode 5 使用 ios 7.0 sdk 构建的。如果我使用 ios 6.1 sdk 构建,但在 ios7 上运行,则会出现同样的问题。使用 ios 6.1 sdk 构建并在 ios 6.1.3 上运行不会导致任何消息和问题。

我的完整应用程序在 ios 7 上 50% 的时间崩溃。我通过从内存中抛出很多东西(主要是图像)来响应内存警告,并且分析证实了这一点,但我仍然收到一连串警告(即它们在之后继续内存被释放)。

如果我使用前置摄像头、从图库中选择或使用 iPad 3,则没有消息。因此,我怀疑内存问题与使用后置摄像头时 UIImagePickerController 的大小有关。

我已经充分探索了stackoverflow,并特别关注了这篇文章——UIImagePickerController error: Snapshoting a view that has not been rendering in iOS 7 result in an empty snapshot in iOS 7

我已经尝试了所有建议,但我的简单测试应用程序排除了大部分解释。

有什么想法吗?我应该放弃对 iPhone 4S 的支持吗?我还没有确认 iPhone 5 上的问题,但我会尽快更新这个问题。

:-)

4

1 回答 1

3

我建议您不要使用图像选择器的属性,而是使用本地对象。请参阅下面我在 IOS7 上也可以正常工作的代码。

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.showsCameraControls = YES;

    [self presentViewController:imagePicker animated:YES completion:nil];
}
于 2013-10-16T06:35:34.223 回答