代码之家  ›  专栏  ›  技术社区  ›  Cocoa Dev

我只想打开闪光灯来缝合照片。我正在尝试使用avcapturedevice和avcaptureflashmodeon。

  •  0
  • Cocoa Dev  · 技术社区  · 14 年前
    -(IBAction)turningFlashOn:(id)sender
    {
    AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
    AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;
    AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];
    
    if (videoInput) {
        [captureSession addInput:videoInput];
    
    
    
        AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init];
        [videoOutput setSampleBufferDelegate:self queue:dispatch_get_current_queue()];
        [captureSession addOutput:videoOutput];
        [captureSession startRunning];
        videoCaptureDevice.torchMode = AVCaptureFlashModeOn;
    }
    }
    

    有人要求我使用lockforconfiguration,但它不起作用,或者我用错了。有人能告诉我我做错了什么吗?

    2 回复  |  直到 13 年前
        1
  •  2
  •   gnuchu    14 年前
    if([videoCaptureDevice lockForConfiguration]) {
      [videoCaptureDevice setTorchMode:AVCaptureTorchModeOn];
      [videoCaptureDevice unlockForConfiguration];
     }
    
        2
  •  1
  •   Nikkie    13 年前
    - (void)flashLightOn {
    
    
        NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
        for (AVCaptureDevice *device in devices) {
            if ([device hasFlash] == YES) {
    
                [device lockForConfiguration:nil];
                [device setTorchMode:AVCaptureTorchModeOn];
                [device unlockForConfiguration];
            }
    
        }
    }
    
    -(void)flashLightOff {
    
    
        NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
        for (AVCaptureDevice *device in devices) {
            if ([device hasFlash] == YES) {
    
                [device lockForConfiguration:nil];
                [device setTorchMode:AVCaptureTorchModeOff];
                [device unlockForConfiguration];
            }
    
        }
    
    }