从相机拍摄图像时闪烁闪光灯

Jig*_*adu 5 iphone ipad

我正在使用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];
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

有什么问题吗?任何其他方法来获得解决方案?在按下使用按钮之前拍摄图像后我也必须停止闪烁.

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

小智 5

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

NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
Run Code Online (Sandbox Code Playgroud)

每秒被调用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];
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)