如何预览livephoto

时间:2017-05-16 05:15:08

标签: ios phlivephoto

我正在做一个申请。在那里我想拍摄实时照片并将其显示给用户进行检查。我使用下面的代码拍摄了实时照片。我得到了输出网址,一旦捕获了实时照片,现在我想在PHLivePhotoView中显示实时照片,我们如何使用输出网址来做到这一点。我也获得了实时照片数据,请参阅下面的委托方法。

- (void)viewDidLoad {
    [super viewDidLoad];
    //Capture Session
    AVCaptureSession *session = [[AVCaptureSession alloc]init];
    session.sessionPreset = AVCaptureSessionPresetPhoto;

    //Add device
    AVCaptureDevice *device =
    [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    //Input
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];

    if (!input)
    {
        NSLog(@"No Input");
    }

    [session addInput:input];

    //Output
     //    AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
     //    [session addOutput:output];
    //    output.videoSettings =
    //    @{ (NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA) };

    AVCapturePhotoOutput *output =[[AVCapturePhotoOutput alloc]init];
    [session addOutput:output];
    output.livePhotoCaptureEnabled = true;
    output.highResolutionCaptureEnabled = YES;
    //Preview Layer
    previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    UIView *myView = self.previewView;
    previewLayer.frame = myView.bounds;
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.previewView.layer addSublayer:previewLayer];

    //Start capture session
    [session startRunning];
    [session commitConfiguration];
    captureOutput = [[AVCapturePhotoOutput alloc]init];
    captureOutput = output;

    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)captureImage:(id)sender
{
    AVCapturePhotoSettings * settings = [AVCapturePhotoSettings photoSettings];
    settings.highResolutionPhotoEnabled = YES;
    settings.flashMode = AVCaptureFlashModeOn;
    NSString *livePhotoMovieFileName = [NSUUID UUID].UUIDString;
    NSString *livePhotoMovieFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[livePhotoMovieFileName stringByAppendingPathExtension:@"mov"]];
    settings.livePhotoMovieFileURL = [NSURL fileURLWithPath:livePhotoMovieFilePath];
    [captureOutput capturePhotoWithSettings:settings delegate:self];
}

- (void)captureOutput:(AVCapturePhotoOutput *)captureOutput didFinishProcessingLivePhotoToMovieFileAtURL:(NSURL *)outputFileURL duration:(CMTime)duration photoDisplayTime:(CMTime)photoDisplayTime resolvedSettings:(AVCaptureResolvedPhotoSettings *)resolvedSettings error:(nullable NSError *)error
   {
      NSLog(@"%@",outputFileURL);
   }

- (void)captureOutput:(AVCapturePhotoOutput *)captureOutput didFinishProcessingPhotoSampleBuffer:(nullable CMSampleBufferRef)photoSampleBuffer previewPhotoSampleBuffer:(nullable CMSampleBufferRef)previewPhotoSampleBuffer resolvedSettings:(AVCaptureResolvedPhotoSettings *)resolvedSettings bracketSettings:(nullable AVCaptureBracketedStillImageSettings *)bracketSettings error:(nullable NSError *)error
{
    photoData = [AVCapturePhotoOutput JPEGPhotoDataRepresentationForJPEGSampleBuffer:photoSampleBuffer previewPhotoSampleBuffer:previewPhotoSampleBuffer];
    NSLog(@"%@",photoData);
}

1 个答案:

答案 0 :(得分:0)

你可以使用

PHLivePhoto.requestLivePhotoWithResourceFileURLs

或者你也可以使用webview