2017-11-08 28 views
3

iPhone Xフラッシュをトーチモードで実行する際に問題があります。iPhone Xフラッシュをタッチモードで起動できません

戻るAVCaptureDeviceTypeBuiltInTelephotoCameraキャプチャデバイスとして選択:私は

[self.captureDevice lockForConfiguration:nil]; 
BOOL result = [self.captureDevice setTorchModeOnWithLevel:1 error:&error]; 
[self.captureDevice unlockForConfiguration]; 
とトーチ・モードにフラッシュ光を切り替えるためにしようとしている

[self.captureDevice isTorchModeSupported:AVCaptureTorchModeOn] 

com.apple.avfoundation.avcapturedevice.built-in_video:2' - 
AVCaptureDeviceTypeBuiltInTelephotoCamera 

でタッチモードの可用性をチェックした後

この呼び出しは成功しました。結果==はい、エラー== nil。しかし、フラッシュライトが一度点滅してから消灯します。

私はiPhone Xでこの動作を確認しましたが、iPhone 8とiPhone 8 Plusの所有者から同じ動作のレポートがあります。いくつかのユーザーは、この問題はiOS 11.1へのアップデート後に現れたと言います。しかし、私は自分自身でそれをiPhone 8で再現することはできませんでした。

この問題を修正またはデバッグする方法はありますか?私のアプリから

完全なコードスニペットは、以下のとおり:

// Retrieve the back camera 
    if ([AVCaptureDeviceDiscoverySession class]) { 
     DDLogDebug(@"Search camera with AVCaptureDeviceDiscoverySession"); 
     AVCaptureDevice* camera = 
     [AVCaptureDeviceDiscoverySession 
     discoverySessionWithDeviceTypes: @[AVCaptureDeviceTypeBuiltInTelephotoCamera] 
     mediaType:AVMediaTypeVideo 
     position:AVCaptureDevicePositionBack].devices.firstObject; 

     if (!camera) { 
      camera = [AVCaptureDeviceDiscoverySession 
         discoverySessionWithDeviceTypes: @[AVCaptureDeviceTypeBuiltInTelephotoCamera] 
         mediaType:AVMediaTypeVideo 
         position:AVCaptureDevicePositionBack].devices.firstObject; 
     } 
     DDLogDebug(@"Did find %@ camera", camera); 
     self.captureDevice = camera; 
    } else { 
     DDLogDebug(@"Haven't found camera device with AVCaptureDeviceDiscoverySession"); 
    } 

    if (!self.captureDevice) { 
     DDLogDebug(@"Searching at [AVCaptureDevice devices], where %lu devices available", (unsigned long)AVCaptureDevice.devices.count); 
     for (AVCaptureDevice *device in [AVCaptureDevice devices]) { 
      if ([device hasMediaType:AVMediaTypeVideo] && [device hasTorch]) { 
       self.captureDevice = device; 
       break; 
      } 
     } 
    } 

    if (!self.captureDevice) { 
     NSError* error = [NSError buildError:^(MRErrorBuilder *builder) { 
      builder.localizedDescription = NSLocalizedString(@"There is no camera devices able to measure heart rate", nil); 
      builder.domain    = kWTCameraHeartRateMonitorError; 
      builder.code     = 27172; 
     }]; 
     DDLogError(@"%@", error); 
     self.session = nil; 
     self.handler(0, 0, error); 
     return NO; 
    } 

    NSError *error; 
    AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc] initWithDevice:self.captureDevice 
                     error:&error]; 
    if (error) { 
     DDLogError(@"%@", error); 
     self.session = nil; 
     self.handler(0, 0, error); 
     return NO; 
    } 


    NSString* deviceType = [self.captureDevice respondsToSelector:@selector(deviceType)] ? self.captureDevice.deviceType : @"Unknown"; 

    DDLogDebug(@"Configurating camera '%@'/'%@' - %@ id %@ at %ld connected: %@", self.captureDevice.localizedName, self.captureDevice.modelID, deviceType, self.captureDevice.uniqueID, (long)self.captureDevice.position, [email protected]"YES":@"NO"); 

    self.session    = [[AVCaptureSession alloc] init]; 
    NSString* preset = [self.session canSetSessionPreset:AVCaptureSessionPresetLow] ? AVCaptureSessionPresetLow : nil; 
    if (preset) { 
     self.session.sessionPreset = preset; 
    } 

    [self.session beginConfiguration]; 
    [self.session addInput:input]; 

    // Find the max frame rate we can get from the given device 
    AVCaptureDeviceFormat *currentFormat; 
    for (AVCaptureDeviceFormat *format in self.captureDevice.formats) 
    { 
     NSArray *ranges = format.videoSupportedFrameRateRanges; 
     AVFrameRateRange *frameRates = ranges[0]; 

     // Find the lowest resolution format at the frame rate we want. 
     if (frameRates.maxFrameRate == FRAMES_PER_SECOND && (!currentFormat || (CMVideoFormatDescriptionGetDimensions(format.formatDescription).width < CMVideoFormatDescriptionGetDimensions(currentFormat.formatDescription).width && CMVideoFormatDescriptionGetDimensions(format.formatDescription).height < CMVideoFormatDescriptionGetDimensions(currentFormat.formatDescription).height))) 
     { 
      currentFormat = format; 
     } 
    } 

    if (![self.captureDevice isTorchModeSupported:AVCaptureTorchModeOn]) { 
     NSError* error = [NSError buildError:^(MRErrorBuilder *builder) { 
      builder.localizedDescription = NSLocalizedString(@"Torch mode is not supported for your camera", nil); 
      builder.domain    = kWTCameraHeartRateMonitorError; 
      builder.code     = 28633; 
     }]; 
     self.session = nil; 
     DDLogError(@"%@", error); 
     self.session = nil; 
     self.handler(0, 0, error); 
     return NO; 
    } 

    // Tell the device to use the max frame rate. 
    [self.captureDevice lockForConfiguration:nil]; 
    DDLogVerbose(@"Turn on tourch mode with level 0.5"); 
    self.captureDevice.flashMode = AVCaptureFlashModeOff; 
    BOOL result = [self.captureDevice setTorchModeOnWithLevel:0.5 error:&error]; 
    if (!result) { 
     DDLogError(@"%@", error); 
     self.session = nil; 
     self.handler(0, 0, error); 
     return NO; 
    } 
    [self.captureDevice setFocusMode:AVCaptureFocusModeLocked]; 
    [self.captureDevice setFocusModeLockedWithLensPosition:1.0 
             completionHandler:nil]; 
    self.captureDevice.activeFormat = currentFormat; 
    self.captureDevice.activeVideoMinFrameDuration = CMTimeMake(1, FRAMES_PER_SECOND); 
    self.captureDevice.activeVideoMaxFrameDuration = CMTimeMake(1, FRAMES_PER_SECOND); 
    [self.captureDevice unlockForConfiguration]; 

    // Set the output 
    AVCaptureVideoDataOutput* videoOutput = [AVCaptureVideoDataOutput new]; 

    // create a queue to run the capture on 
    dispatch_queue_t captureQueue=dispatch_queue_create("catpureQueue", DISPATCH_QUEUE_SERIAL); 

    // setup our delegate 
    [videoOutput setSampleBufferDelegate:self queue:captureQueue]; 

    // configure the pixel format 

    videoOutput.videoSettings = @{(id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA)}; 
    videoOutput.alwaysDiscardsLateVideoFrames = NO; 

    [self.session addOutput:videoOutput]; 

    if (debugPath) { 
     NSError* error; 
     [[NSFileManager defaultManager] removeItemAtPath:debugPath 
                error:nil]; 

     BOOL result = 
     [[NSFileManager defaultManager] createDirectoryAtPath:debugPath 
            withIntermediateDirectories:YES 
                attributes:nil 
                 error:&error]; 
     if (result) { 
      [self setupDebugRecordAt:debugPath withFormat:currentFormat]; 
     } else { 
      DDLogError(@"%@", error); 
     } 

     const char* path = [debugPath cStringUsingEncoding:NSUTF8StringEncoding]; 
     self.filter->setDebugPath(path); 
    } 


    // Start the video session 
    [self.session commitConfiguration]; 

    self.frameNumber = 0; 
    [self.assetWriter startWriting]; 
    [self.assetWriter startSessionAtSourceTime:kCMTimeZero]; 
    [self.session startRunning]; 
+0

私は同様の問題を持っていますが、iPhoneのiOS 8 11.1.1に。私のユーザーの何人かはバグを報告していますが、私は自分のiPhone 8やXではないので、解決するのは非常に難しいです。解決策を見つけたら更新してください。 – DzungPV

+0

こんにちは、@ DzungPV。私は私のプロジェクトでこの問題を解決しました。私の解決策を以下でお読みください。これがあなたを助けることを願っています。 – lazarev

答えて

3

最後に、問題が固定されています。私は正確な理由がわからない。この問題に関連する情報は高く評価されます。

この問題は、iOS 11.1を実行しているiPhone 8,8+およびiPhone Xに存在します。フォームiOSを11.0から11.1にアップデートした後、iPhone 8でこの動作を再現しました。

私が気づいたことはトーチが

BOOL result = [self.captureDevice setTorchModeOnWithLevel:0.5 error:&error]; 

を呼び出した後にオンになり、だから、解決策はどこALLトーチの設定を実行することでした

[self.captureDevice setFocusMode:AVCaptureFocusModeLocked]; 

または

[self.session commitConfiguration]; 

後に消灯していることです他のセッションとデバイスの設定が完了し、セッションが開始されます。

私の現在の実装は次のようになります。

// Session configuration ... 

[self.session startRunning]; 

if (![self.captureDevice isTorchModeSupported:AVCaptureTorchModeOn]) { 
    NSError* error = [NSError buildError:^(MRErrorBuilder *builder) { 
     builder.localizedDescription = NSLocalizedString(@"Torch mode is not supported for your camera", nil); 
     builder.domain    = kWTCameraHeartRateMonitorError; 
     builder.code     = 28633; 
    }]; 
    DDLogError(@"%@", error); 
    if (self.session) { 
     [self.session stopRunning]; 
    } 
    self.session = nil; 
    self.handler(0, 0, error); 
    return NO; 
} 

[self.captureDevice lockForConfiguration:nil]; 
self.captureDevice.flashMode = AVCaptureFlashModeOff; 
[self.captureDevice setFocusMode:AVCaptureFocusModeLocked]; 
[self.captureDevice setFocusModeLockedWithLensPosition:1.0 
             completionHandler:nil]; 
self.captureDevice.activeFormat = currentFormat; 
self.captureDevice.activeVideoMinFrameDuration = CMTimeMake(1, FRAMES_PER_SECOND); 
self.captureDevice.activeVideoMaxFrameDuration = CMTimeMake(1, FRAMES_PER_SECOND); 

// This call should be placed AFTER all other configurations 

BOOL result = [self.captureDevice setTorchModeOnWithLevel:0.5 error:&error]; 
if (!result) { 
    DDLogError(@"%@", error); 
    self.session = nil; 
    self.handler(0, 0, error); 
    return NO; 
} 
[self.captureDevice unlockForConfiguration]; 
+0

解決していただきありがとうございますが、廃止予定のプロパティを使用すると、self.captureDevice.torchMode = AVCaptureTorchModeOffに変更できます。 – DzungPV

+0

どちらが推奨されていませんか? – lazarev

+0

この1つ:https://developer.apple.com/documentation/avfoundation/avcapturedevice/1388116-flashmode – DzungPV