How To Use AVCaptureStillImageOutput To Take Picture

前端 未结 4 1646
挽巷
挽巷 2021-02-04 08:35

I have a preview layer that is pulling from the camera and working as it should. I would like to be able to take a picture when I press a button. I have inited the AVCaptureStil

4条回答
  •  别跟我提以往
    2021-02-04 09:03

    -(void)captureImage:(NSString *)string successCallback:(void (^)(id))successCallback errorCallback:(void (^)(NSString *))errorCallback{
    
        __block UIImage *image;
        AVCaptureConnection *videoConnection = nil;
        for (AVCaptureConnection *connection in stillImageOutput.connections)
        {
            for (AVCaptureInputPort *port in [connection inputPorts])
            {
                if ([[port mediaType] isEqual:AVMediaTypeVideo] )
                {
                    videoConnection = connection;
                    break;
                }
            }
            if (videoConnection)
            {
                break;
            }
        }
    
        //NSLog(@"about to request a capture from: %@", stillImageOutput);
        [videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
    
        [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
         {
             CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
             if (exifAttachments)
             {
                 // Do something with the attachments.
                 //NSLog(@"attachements: %@", exifAttachments);
             } else {
                 //NSLog(@"no attachments");
             }
    
             NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
             image = [[UIImage alloc] initWithData:imageData];
    
    
             successCallback(image);
             //UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
         }];
    
    
        NSError *error;
        if (error) {
            errorCallback(@"error");
        }else{
    
        }
    }
    

提交回复
热议问题