调用相机后iPhone会随机崩溃吗?

时间:2012-07-12 15:11:41

标签: ios objective-c memory-management crash

我使用仪器测试了我的应用程序,它显示以下泄漏。 enter image description here

当我双击堆栈跟踪中的CameraVC时,它会在我的代码中引用以下行。 enter image description here

当我打电话给相机时会发生这种情况,我使用以下代码调用它: -

      - (IBAction) getCamera
{
    // set up our camera overlay view   
    // tool bar - handy if you want to be able to exit from the image picker...
    UIToolbar *toolBar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 480-44, 320, 44)];

    UIBarButtonItem *spaceItem  = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace  target:nil action:nil] autorelease];
    UIBarButtonItem *spaceItem1  = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace  target:nil action:nil] autorelease];
    UIBarButtonItem *cancelItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel  target:self action:@selector(cancelPickingImag)] autorelease];
    UIBarButtonItem *cameraItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera  target:self action:@selector(finishedAugmentedReality)] autorelease]; 

    spaceItem.width  = 2.0;
    spaceItem1.width = 55.0;
    NSArray *items=[NSArray arrayWithObjects:spaceItem,cancelItem,spaceItem1,cameraItem,nil];
    [toolBar setItems:items];

    // create the overlay view
    overlayView=[[OverlayView alloc] initWithFrame:CGRectMake(0, 300, 320, 480-44)];

    // important - it needs to be transparent so the camera preview shows through!
    overlayView.opaque=NO;
    overlayView.backgroundColor=[UIColor clearColor];

    // parent view for our overlay
    UIView *parentView=[[UIView alloc] initWithFrame:CGRectMake(0,0,320, 480)];
    [parentView addSubview:overlayView];
    [parentView addSubview:toolBar];
    [parentView addSubview:lbl];
    [parentView addSubview:overlayGraphicView];

    // configure the image picker with our overlay view
    //UIImagePickerController *picker=[[UIImagePickerController alloc] init];

    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeCamera])
    {
        imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

        // hide the camera controls
        imagePicker.showsCameraControls=NO;

        imagePicker.delegate = self;
        //imagePicker.allowsImageEditing = NO;

        // and put our overlay view in
        imagePicker.cameraOverlayView=parentView;
    }
    else
    {
        imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                  (NSString *) kUTTypeImage,
                                  nil];
        imagePicker.allowsEditing = NO;

    }

    [self presentModalViewController:imagePicker animated:YES];

    //Free memory 
    [imagePicker release];//,imagePicker =nil;
    [parentView release], parentView=nil;
    [overlayGraphicView release], overlayGraphicView= nil;
    [lbl release], lbl=nil;
    [overlayView release];//, overlayView =nil;
    [toolBar release], toolBar=nil;
}

任何帮助都非常感谢

由于

1 个答案:

答案 0 :(得分:0)

正在使用parentView创建

[alloc [init...]],这意味着它是retain。然后,您将其分配到cameraOverlayView的{​​{1}}属性,这意味着它再次获得imagePicker。完成该任务后,您需要retain

(这几乎可以肯定是“泄密”的原因,但不会导致“崩溃”。你说你遇到了崩溃,但根本没有描述。)