Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Objective-C拍摄照片的简单方法,无需摄像头接口。只需从相机中获取一张图片并保存到文件中即可_Ios_Iphone_Ipad_Camera - Fatal编程技术网

Ios Objective-C拍摄照片的简单方法,无需摄像头接口。只需从相机中获取一张图片并保存到文件中即可

Ios Objective-C拍摄照片的简单方法,无需摄像头接口。只需从相机中获取一张图片并保存到文件中即可,ios,iphone,ipad,camera,Ios,Iphone,Ipad,Camera,没有相机接口,我找不到简单的拍照方法。 我只需要从相机中获取一张照片并将其保存到文件中。我使用此代码使用前置相机拍照。并非所有的代码都是我的,但我没有找到指向原始源代码的链接。该代码还产生快门声音。图像质量不是很好(非常暗),所以代码需要调整一两次 -(void) takePhoto { AVCaptureDevice *frontalCamera; NSArray *allCameras = [AVCaptureDevice devicesWithMediaType:AVM

没有相机接口,我找不到简单的拍照方法。
我只需要从相机中获取一张照片并将其保存到文件中。

我使用此代码使用前置相机拍照。并非所有的代码都是我的,但我没有找到指向原始源代码的链接。该代码还产生快门声音。图像质量不是很好(非常暗),所以代码需要调整一两次

-(void) takePhoto 
{
    AVCaptureDevice *frontalCamera;

    NSArray *allCameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

    for ( int i = 0; i < allCameras.count; i++ )
    {
        AVCaptureDevice *camera = [allCameras objectAtIndex:i];

        if ( camera.position == AVCaptureDevicePositionFront )
        {
            frontalCamera = camera;
        }
    }

    if ( frontalCamera != nil )
    {
        photoSession = [[AVCaptureSession alloc] init];

        NSError *error;
        AVCaptureDeviceInput *input =
        [AVCaptureDeviceInput deviceInputWithDevice:frontalCamera error:&error];

        if ( !error && [photoSession canAddInput:input] )
        {
            [photoSession addInput:input];

            AVCaptureStillImageOutput *output = [[AVCaptureStillImageOutput alloc] init];

            [output setOutputSettings:
             [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil]];

            if ( [photoSession canAddOutput:output] )
            {
                [photoSession addOutput:output];

                AVCaptureConnection *videoConnection = nil;

                for (AVCaptureConnection *connection in output.connections)
                {
                    for (AVCaptureInputPort *port in [connection inputPorts])
                    {
                        if ([[port mediaType] isEqual:AVMediaTypeVideo] )
                        {
                            videoConnection = connection;
                            break;
                        }
                    }
                    if (videoConnection) { break; }
                }

                if ( videoConnection )
                {
                    [photoSession startRunning];

                    [output captureStillImageAsynchronouslyFromConnection:videoConnection
                                                        completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

                        if (imageDataSampleBuffer != NULL)
                        {
                            NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
                            UIImage *photo = [[UIImage alloc] initWithData:imageData];
                            [self processImage:photo]; //this is a custom method
                        }
                    }];
                }
            }
        }
    }
}
如果应用程序不能接受延迟,您可以将代码分成两部分,然后在
viewdide
(或类似位置)启动
photoSession
,并在需要时(通常在一些用户交互之后)立即拍摄快照

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.25 * NSEC_PER_SEC);
也会产生一个好的结果-因此不需要整个第二个滞后


请注意,编写此代码是为了使用前置相机拍照-我相信如果需要使用后置相机,您将知道如何修复它。

我使用此代码使用前置相机拍照。并非所有的代码都是我的,但我没有找到指向原始源代码的链接。该代码还产生快门声音。图像质量不是很好(非常暗),所以代码需要调整一两次

-(void) takePhoto 
{
    AVCaptureDevice *frontalCamera;

    NSArray *allCameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

    for ( int i = 0; i < allCameras.count; i++ )
    {
        AVCaptureDevice *camera = [allCameras objectAtIndex:i];

        if ( camera.position == AVCaptureDevicePositionFront )
        {
            frontalCamera = camera;
        }
    }

    if ( frontalCamera != nil )
    {
        photoSession = [[AVCaptureSession alloc] init];

        NSError *error;
        AVCaptureDeviceInput *input =
        [AVCaptureDeviceInput deviceInputWithDevice:frontalCamera error:&error];

        if ( !error && [photoSession canAddInput:input] )
        {
            [photoSession addInput:input];

            AVCaptureStillImageOutput *output = [[AVCaptureStillImageOutput alloc] init];

            [output setOutputSettings:
             [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil]];

            if ( [photoSession canAddOutput:output] )
            {
                [photoSession addOutput:output];

                AVCaptureConnection *videoConnection = nil;

                for (AVCaptureConnection *connection in output.connections)
                {
                    for (AVCaptureInputPort *port in [connection inputPorts])
                    {
                        if ([[port mediaType] isEqual:AVMediaTypeVideo] )
                        {
                            videoConnection = connection;
                            break;
                        }
                    }
                    if (videoConnection) { break; }
                }

                if ( videoConnection )
                {
                    [photoSession startRunning];

                    [output captureStillImageAsynchronouslyFromConnection:videoConnection
                                                        completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

                        if (imageDataSampleBuffer != NULL)
                        {
                            NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
                            UIImage *photo = [[UIImage alloc] initWithData:imageData];
                            [self processImage:photo]; //this is a custom method
                        }
                    }];
                }
            }
        }
    }
}
如果应用程序不能接受延迟,您可以将代码分成两部分,然后在
viewdide
(或类似位置)启动
photoSession
,并在需要时(通常在一些用户交互之后)立即拍摄快照

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.25 * NSEC_PER_SEC);
也会产生一个好的结果-因此不需要整个第二个滞后


请注意,此代码是为使用前置摄像头拍照而编写的-我相信如果需要使用后置摄像头,您将知道如何修复它。

如果要确保应用程序应用商店的安全,您将无法找到其他解决方案,只能找到官方API。如果要确保应用程序应用商店的安全,则可能会复制,您将找不到其他解决方案,但找到的是官方API。可能是的副本