简体   繁体   中英

How to preview the livephoto

I am doing one application. In that I want to capture the live photo and show it to user for checking. I captured the live photo using below code. I got the output url once capture the live photo and now I want to show that live photo in PHLivePhotoView , how we can do that using output url. And I am getting the live photo data also, see the delegate methods below.

- (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);
}

you can use

PHLivePhoto.requestLivePhotoWithResourceFileURLs

or u can use webview too

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM