iOS使用图像选择器Memory Leak拍照

时间:2013-12-08 10:08:28

标签: ios uiimagepickercontroller

我在这里看到很多关于用这个api拍照的问题

但我遇到了具体问题

以下代码

.h文件

   @interface ComposeMViewController : UIViewController <UITextFieldDelegate , UITableViewDelegate ,UITableViewDataSource, UIImagePickerControllerDelegate , UINavigationControllerDelegate , MFMailComposeViewControllerDelegate , MFMessageComposeViewControllerDelegate , DistributionListViewControllerDelegate,UITextViewDelegate>

in .m

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        //UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        _imagePicker.delegate = self;
        _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

        [self presentViewController:_imagePicker animated:YES completion:nil];
    } else {
        NSLog(@"Camera not available");
    }


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    NSLog(@"At 1");
    UIImage *fullImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    _attachment = [NSMutableDictionary dictionary];
    NSData* imgData = UIImageJPEGRepresentation(fullImage,0.0);
    [_attachment setObject:@"atachmentINFOR.jpg" forKey:@"ImageName"];
    [_attachment setObject:imgData forKey:@"ImageData"];
    NSLog(@"%@",info);
    [self dismissViewControllerAnimated:YES completion:nil];//warning disabled
}

我现在收到记忆警告,有人可以知道为什么吗?

3 个答案:

答案 0 :(得分:1)

最可能的解释是你的UIViewController不响应UINavigationControllerDelegate或UIImagePickerControllerDelegate(它必须响应两者才能成为UIImagePickerController的委托。

您的第二次尝试未编译,因为SourceType不是UIImagePickerController的属性 - 它是sourceType。按照惯例,Objective-C / Cocoa在变量和属性名称的开头使用小写字母。

答案 1 :(得分:1)

您应该始终检查文档中是否有可用的源类型。正如@Programming托马斯所说,确保UIImagePickerControllerDelegate,UINavigationControllerDelegate委托已经设置..

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

        [self presentViewController:imagePickerController animated:YES completion:nil];
    } else {
        NSLog(@"Camera not available");
    }

如果您碰巧收到错误消息,例如“警告:尝试在其视图中显示不在窗口层次结构中!”通常在viewContoller尚未完全加载但等待一秒左右时发生。也许在半秒后使用调度计时器

答案 2 :(得分:0)

这可能对其他人有所帮助 - 我知道这是一个老线程,但我自己在相机方面遇到了内存泄漏问题。我在它上面运行了仪器(会推荐)并发现负责的框架是:[UIImagePickerController viewWillDisappear:]。这属于UIKit库,所以我担心你无法纠正内存泄漏。它是UIKit中的一个错误。我在iPhone 5s上使用iOS7.1。我建议使用一个内存有效的库,例如可以在GitHub上找到的SimpleCam。