拍照时 iOS 应用程序崩溃

标签 ios xcode memory crash uiimagepickercontroller

由于内存问题,我的应用程序在拍照时崩溃。

现在我尝试使用仪器分析我的内存,实际上当应用程序崩溃时我最多使用 23 MB。我将它与那个 Facebook 应用程序进行了比较,它使用了超过 70 MB,所以我不知道为什么我会收到内存警告...

我使用以下代码:

OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.cameraOverlayView = overlay;
[self presentViewController:picker animated:NO completion:nil];

注意:我在没有覆盖 View 的情况下尝试过,它似乎没有改变 我试过带动画和不带动画。

顺便说一句,应用程序在显示图像选择器时不会崩溃,但是当显示图像选择器时,我会收到多个内存警告。 该应用程序在拍照或点击“使用”按钮时崩溃。有时应用程序根本不会崩溃,所以我很困惑。

也许我需要在 didReceiveMemoryWarning 方法中做一些事情,但我不知道该做什么,也许你可以帮我解决这个问题!

Xcode 显示了这个:

memory issue

更新:

这是我得到的

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImagePickerControllerMediaMetadata =     {
    DPIHeight = 72;
    DPIWidth = 72;
    Orientation = 6;
    "{Exif}" =         {
        ApertureValue = "2.526068811667588";
        BrightnessValue = "3.440519128732857";
        ColorSpace = 1;
        DateTimeDigitized = "2013:09:26 14:33:48";
        DateTimeOriginal = "2013:09:26 14:33:48";
        ExposureMode = 0;
        ExposureProgram = 2;
        ExposureTime = "0.03333333333333333";
        FNumber = "2.4";
        Flash = 24;
        FocalLenIn35mmFilm = 35;
        FocalLength = "4.28";
        ISOSpeedRatings =             (
            50
        );
        LensMake = Apple;
        LensModel = "iPhone 4S back camera 4.28mm f/2.4";
        LensSpecification =             (
            "4.28",
            "4.28",
            "2.4",
            "2.4"
        );
        MeteringMode = 5;
        PixelXDimension = 3264;
        PixelYDimension = 2448;
        SceneType = 1;
        SensingMethod = 2;
        ShutterSpeedValue = "4.906905022631062";
        SubjectArea =             (
            1631,
            1223,
            881,
            881
        );
        SubsecTimeDigitized = 551;
        SubsecTimeOriginal = 551;
        WhiteBalance = 0;
    };
    "{MakerApple}" =         {
        1 = 0;
        3 =             {
            epoch = 0;
            flags = 1;
            timescale = 1000000000;
            value = 779858689639583;
        };
        4 = 0;
        5 = 128;
        6 = 138;
        7 = 0;
    };
    "{TIFF}" =         {
        DateTime = "2013:09:26 14:33:48";
        Make = Apple;
        Model = "iPhone 4S";
        Software = "7.0";
        XResolution = 72;
        YResolution = 72;
    };
};
UIImagePickerControllerMediaType = "public.image";
UIImagePickerControllerOriginalImage = "<UIImage: 0x15668c60>";

最佳答案

不要担心内存警告。如果您在 iPhone 4S 或更早的设备上使用 UIImagePickerController,您通常会收到此警告,您对此无能为力。

重要的是,您需要确保通过 imagePickerController:didFinishPickingMediaWithInfo: 的委托(delegate)方法适本地调整图像大小。如果您尝试保存和使用(例如在 UIImageView 中显示)原始图像,由于内存压力,您将在这些旧设备上崩溃。

查看此 excellent blog post有关调整 UIImage 大小的正确方法的讨论。

具体来说,您会发现此类别有助于正确调整图像大小:

UIImage+Resize.h

UIImage+Resize.m

如果您已经使用了另一种有效的方法,那就太好了。否则,此方法非常有效。

使用上述类别方法,这里是 imagePickerController:didFinishPickingMediaWithInfo: 使用 GCD 在后台线程上调整大小的示例:

- (void)imagePickerController:(UIImagePickerController *)imagePicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Dismiss the image picker first to free its memory
    [self dismissViewControllerAnimated:YES completion:nil];

    UIImage *originalImage = info[UIImagePickerControllerOriginalImage];

    if (!originalImage)
        return;

    // Optionally set a placeholder image here while resizing happens in background

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // Set desired maximum height and calculate width
        CGFloat height = 640.0f;
        CGFloat width = (height / self.size.height) * self.size.width;

        UIImage * image = [originalImage resizedImage:CGSizeMake(width, height) interpolationQuality:kCGInterpolationDefault];

        // Save and use this image
    });
}

关于拍照时 iOS 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19027869/

相关文章:

ios - 采取一定数量的步骤后通知。 swift 2.0

ios - 尝试创建 use_block_in_frame 是什么意思

ios - UIBarButtonItem 的函数

iphone - 如何识别一个对象被改变了?

swift - 如何将 Swift 连接到托管在 Google Cloud 上的 postgresql 数据库

c - C 初学者 : strings and memory

c++ - OpenGL/Carbon/Cocoa 内存管理自动释放问题

c++ - 递归树算法中的内存使用

ios - 我的数组的 IndexPath 有问题吗? swift 4 火力基地

ios - 如何为在 iPad 上运行的 iPhone SwiftUI 应用程序锁定纵向方向?