3

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

现在我尝试用仪器分析我的内存,实际上当应用程序崩溃时我最多使用 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];

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

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

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

}

4

2 回答 2

3

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

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

有关调整UIImage. _

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

UIImage+调整大小.h

UIImage+调整大小.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
    });
}
于 2013-09-26T12:31:45.733 回答
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);

});}
于 2015-03-16T05:17:47.973 回答