拍摄照片时iOS App崩溃

时间:2013-09-26 12:09:30

标签: ios xcode memory crash uiimagepickercontroller

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

现在我尝试用仪器分析我的记忆,实际上当应用程序崩溃时我使用的最大值为23 MB。我将它与Facebook App进行了比较,它使用了超过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];

注意:我在没有叠加视图的情况下尝试了它,但似乎没有改变 我尝试使用和不使用动画。

BTW应用程序在呈现图像拾取器时不会崩溃,但是当显示图像拾取器时,我会收到多个内存警告。 应用程序在拍摄照片或点击“使用”按钮时崩溃。有时应用程序根本没有崩溃所以我很困惑。

也许我需要在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>";

}

2 个答案:

答案 0 :(得分:3)

不要担心内存警告。如果您在iPhone 4S或旧版设备上使用UIImagePickerController,通常会收到此警告,但您无法对其进行太多操作。

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

有关调整UIImage大小的正确方法的讨论,请参阅此excellent blog post

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

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
    });
}

答案 1 :(得分:0)

我也遇到了同样的问题,我找到了一个补丁,以尽量减少问题。所以我做的只是阻止主线程,直到图像选择器被解雇。是的。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
__weak typeof(picker) wSelf = picker;
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

[wSelf dismissViewControllerAnimated:YES completion:^{
    @try {


        [self saveImage:info];
    }
    @catch (NSException *exception) {
        NSLog(@"Exception:%@",exception);
    }
    @finally {
    }

}];});}

-(void)saveImage:(NSDictionary *)info{
 [KAppdelegate ShowLoader];

  UIImage *image=(UIImage *)[info objectForKey:@"UIImagePickerControllerOriginalImage"];

  ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
 dispatch_queue_t queue =  dispatch_queue_create("com.myApp.saveToCameraRoll", NULL);
dispatch_async(queue, ^{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
[library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){

    if (error) {
        // handle the error
    }
    else {
        dispatch_async(dispatch_get_main_queue(), ^{
            // perhaps indicate to the user that save has finished

        });
    }

    dispatch_semaphore_signal(sema);

}];

dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

});}
相关问题