拍摄照片时iOS应用程式崩溃 [英] iOS App crashes when taking a photo

查看:270
本文介绍了拍摄照片时iOS应用程式崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于内存问题,我的应用程式在拍照时失败。



现在我试图用乐器分析我的记忆,实际上我使用的是最大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];

注意:我没有重叠视图,似乎更改
我尝试了它和没有动画。



应用程序在呈现imagepicker时不会崩溃,但是当imagepicker显示时,我收到多个内存警告。
当拍摄照片或点击使用按钮时,应用程序崩溃。有时,应用程序不会崩溃,所以我很困惑。



也许我需要在didReceiveMemoryWarning方法中做一些事情,但我不知道什么,也许你可以帮助我!



Xcode显示:





更新



这是我从

   - (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后置摄像头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 ,通常会收到此警告,您不能做太多。

$ b重要的是,您需要确保您通过 imagePickerController:didFinishPickingMediaWithInfo:的委托方法调整图像大小。如果您尝试保存并使用(例如在 UIImageView 中显示)原始图片,您会因为内存压力而在这些旧设备上崩溃。



查看此 UIImage 大小的正确方法的讨论。



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



UIImage + Resize.h



< a href =http://vocaro.com/trevor/blog/wp-content/uploads/2009/10/UIImage+Resize.m =nofollow> UIImage + Resize.m



如果你已经使用另一种方法,很好。



使用所述类别方法,下面是一个 imagePickerController:didFinishPickingMediaWithInfo:使用示例GCD在后台线程上执行调整大小:

   - (void)imagePickerController:(UIImagePickerController *)imagePicker didFinishPickingMediaWithInfo:(NSDictionary * )info 
{
//首先取消图片选择器释放内存
[self dismissViewControllerAnimated:YES completion:nil];

UIImage * originalImage = info [UIImagePickerControllerOriginalImage];

if(!originalImage)
return;

//可以在后台调整大小时调整占位符图片

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^ {

//设置所需的最大高度并计算宽度
CGFloat height = 640.0f;
CGFloat width =(height / self.size.height)* self.size.width;

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

//保存并使用此图片
}
}


my App crashes when taking a photo due to a memory issue.

Now I tried to analyze my memory with instruments and actually I am using a maximum of 23 MB when the app crashes. I compared it with that Facebook App and it uses more than 70 MB so I do not have an idea why I am getting memory warnings...

I use the following code:

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];

Note: I have tried it without the overlay view and it did not seem to change and I tried it with and without animation.

BTW the app does not crash when presenting the imagepicker, but when the imagepicker is shown I receive multiple memory warnings. The App crashes either when taking the photo or when hitting the 'use' button. Sometimes the App does not crash at all so I am very confused.

Maybe I need to do something in the didReceiveMemoryWarning method but I do not know what, maybe you can help me out with that!

Xcode shows this:

Update:

This is what i get from the

-(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>";

}

解决方案

Don't worry about the memory warning. If you're using UIImagePickerController on iPhone 4S or older devices, you'll typically get this warning, and you can't do much about it.

What's important is that you need to make sure you're resizing the image appropriately via your delegate method for imagePickerController:didFinishPickingMediaWithInfo:. If you try to save and use (such as displaying in UIImageView) the original image, you will crash on these older devices due to memory pressure.

See this excellent blog post for a discussion on the correct way to resize a UIImage.

Specifically, you will find this category to be helpful for resizing images correctly:

UIImage+Resize.h

UIImage+Resize.m

If you already use another method that works, great. Otherwise, this method works very well.

Using said category methods, here's an example of imagePickerController:didFinishPickingMediaWithInfo: using GCD to do the resizing on a background thread:

- (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应用程式崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆