5

我正在使用 AVCapture 框架在从相机拍照时设置闪烁的闪光灯。在这种方法中,我得到了几秒钟的闪光灯闪烁效果,但随后它就崩溃了。

下面是我已经完成的代码。

-(IBAction) a
{
    _picker = [[UIImagePickerController alloc] init];
    _picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    _picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    _picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
    _picker.showsCameraControls = YES;
    _picker.navigationBarHidden =YES;
    _picker.toolbarHidden = YES;
    _picker.wantsFullScreenLayout = YES;

    [_picker takePicture];

    // Insert the overlay
    OverlayViewController *overlay = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
    overlay.pickerReference = _picker;
    _picker.cameraOverlayView = overlay.view;
    _picker.delegate = (id)self;

    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(flashLight_On_Off) userInfo:nil repeats:YES];

    [self presentModalViewController:_picker animated:NO];
}

- (void)flashLight_On_Off
{
    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *device in devices)
    {
        if ([device hasFlash] == YES)
        {
            [device lockForConfiguration:nil];
            if (bulPicker == FALSE)
            {
                [device setTorchMode:AVCaptureTorchModeOn];
                bulPicker = TRUE;
            }
            else
            {
                [device setTorchMode:AVCaptureTorchModeOff];
                bulPicker = FALSE;
            }
            [device unlockForConfiguration];
        }

    }
}

有什么问题吗?还有其他方法可以得到解决方案吗?在按下使用按钮之前,我还必须在拍摄图像后停止闪烁。

请建议我适当的解决方案。

4

1 回答 1

5

如果我没记错 Cocoa 命名约定,除了- alloc,之外- copy的任何方法- mutableCopy都会返回一个自动释放的对象。在这种情况下,

NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

每秒被调用 10 次,每次都自动释放。这意味着它可能不会立即发布,因此它会开始占用您的 RAM,并且操作系统最终会检测到这一点并终止您的应用程序的进程。

如果您事先知道它们会被大量调用,那么您应该做的是将这些类型的操作包装到一个自动释放池中。

- (void)toggleFlashlight
{
    @autoreleasepool {
        NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
        for (AVCaptureDevice *device in devices)
        {
            if ([device hasFlash]) {
                [device lockForConfiguration:nil];
                if (bulPicker) {
                    [device setTorchMode:AVCaptureTorchModeOff];
                    bulPicker = NO;
                } else  {
                    [device setTorchMode:AVCaptureTorchModeOn];
                    bulPicker = YES;
                }
                [device unlockForConfiguration];
            }
        }
    }
}
于 2012-11-26T12:22:22.023 回答