简体   繁体   English

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

[英]iOS take photo with image picker Memory Leak

I have seen many questions here about taking photo with this api 我在这里看到过很多与此API拍照有关的问题

but I run into specific problem 但我遇到了具体问题

with following code 用下面的代码

.h file .h文件

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

in .m 在.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
}

I am receiving memory warning now , can some1 know why ? 我现在正在收到内存警告,有人可以知道为什么吗?

The most likely explanation is that your UIViewController doesn't respond to the UINavigationControllerDelegate or the UIImagePickerControllerDelegate (it must respond to both in order to be the delegate for the UIImagePickerController. 最可能的解释是,您的UIViewController不响应UINavigationControllerDelegate或UIImagePickerControllerDelegate(它必须同时响应两者才能成为UIImagePickerController的委托。

Your second attempt isn't compiling because SourceType isn't a property of UIImagePickerController - it is sourceType . 您的第二次尝试未编译,因为SourceType不是UIImagePickerController的属性-它是sourceType By convention, Objective-C/Cocoa uses lowercase letters at the beginning of variable and property names. 按照约定,Objective-C / Cocoa在变量和属性名称的开头使用小写字母。

You should always check if source type is available per to the documentation. 您应始终检查文档中是否提供了源类型。 As @Programming Thomas said make sure UIImagePickerControllerDelegate, UINavigationControllerDelegate delegate have been set.. 正如@Programming Thomas所说,请确保已设置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");
    }

if you happen to get a error message like "Warning: Attempt to present on whose view is not in the window hierarchy!" 如果您碰巧收到诸如“警告:尝试在其视图不在窗口层次结构中显示的提示!”之类的错误消息! usually happens when viewContoller has not completely loaded yet wait for a second or so. 通常在viewContoller尚未完全加载但等待一秒钟左右时发生。 maybe to use dispatch timer after half a second 也许半秒钟后使用调度计时器

This might help someone else - I know this is an old thread but I've been experiencing a memory leak myself in regards to the camera. 这可能会对其他人有所帮助-我知道这是一个旧线程,但是我自己一直在遇到与相机有关的内存泄漏。 I ran instruments on it (would recommend) and found that the responsible frame is: [UIImagePickerController viewWillDisappear:] . 我在上面运行了仪器(建议这样做),发现负责的帧是: [UIImagePickerController viewWillDisappear:] This belongs to the UIKit library so I am afraid there is little you can do to correct the memory leak. 这属于UIKit库,因此恐怕您无能为力,无法纠正内存泄漏。 Its a bug in UIKit . 它是UIKit的错误。 Im using iOS7.1 on the iPhone 5s. 我在iPhone 5s上使用iOS7.1。 I would recommend using a memory efficient library,such as SimpleCam that can be found on GitHub. 我建议使用内存有效的库,例如可以在GitHub上找到的SimpleCam。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM