菜鸟教程小白 发表于 2022-12-12 15:24:54

ios - iPhone 相机访问权限?


                                            <p><p>我想知道如何访问 iphone 相机并实时使用它:例如,只需在相机 View 上绘图。</p>

<p><strong>另一个相关问题:</strong></p>

<p>我可以像在 Mac 上的“Photo Booth”中那样一次显示 <strong>4 个相机 View </strong>吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以使用 AVFoundation 来做到这一点</p>

<pre><code>- (void)initCapture {

    AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput
                                          deviceInputWithDevice:
                                          error:nil];

    AVCaptureVideoDataOutput *captureOutput = [ init];

    captureOutput.alwaysDiscardsLateVideoFrames = YES;

    dispatch_queue_t queue;
    queue = dispatch_queue_create(&#34;cameraQueue&#34;, NULL);
    ;
    dispatch_release(queue);

    NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
    NSNumber* value = ;
    NSDictionary* videoSettings = ;
    ;


    self.captureSession = [ init];
    ;

    ;
    ;

    ;

    self.customLayer = ;

    self.customLayer.frame =CGRectMake(5-25,25, 200,150);

    self.customLayer.transform = CATransform3DRotate(CATransform3DIdentity, M_PI/2.0f, 0, 0, 1);

    //self.customLayer.transform =CATransform3DMakeRotation(M_PI/2.0f, 0, 0, 1);


    //;
    //self.customLayer.frame =CGRectMake(0, 0, 200,150);
    //self.customLayer.contentsGravity = kCAGravityResizeAspectFill;

    ;
    //;


    self.customLayer1 = ;
    //self.customLayer.frame = self.view.bounds;
    self.customLayer1.frame =CGRectMake(165-25, 25, 200, 150);
    self.customLayer1.transform = CATransform3DRotate(CATransform3DIdentity, M_PI/2.0f, 0, 0, 1);
    //self.customLayer1.contentsGravity = kCAGravityResizeAspectFill;
    ;




    self.customLayer2 = ;
    //self.customLayer.frame = self.view.bounds;
    self.customLayer2.frame =CGRectMake(5-25, 210 +25, 200, 150);
    self.customLayer2.transform = CATransform3DRotate(CATransform3DIdentity, M_PI/2.0f, 0, 0, 1);
    //self.customLayer1.contentsGravity = kCAGravityResizeAspectFill;
    ;


    self.customLayer3 = ;
    //self.customLayer.frame = self.view.bounds;
    self.customLayer3.frame =CGRectMake(165-25, 210 +25, 200, 150);
    self.customLayer3.transform = CATransform3DRotate(CATransform3DIdentity, M_PI/2.0f, 0, 0, 1);
    //self.customLayer1.contentsGravity = kCAGravityResizeAspectFill;
    ;



}



#pragma mark -
#pragma mark AVCaptureSession delegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
       fromConnection:(AVCaptureConnection *)connection
{


    NSAutoreleasePool * pool = [ init];

    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    /*Lock the image buffer*/
    CVPixelBufferLockBaseAddress(imageBuffer,0);
    /*Get information about the image*/
    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);


    /*Create a CGImageRef from the CVImageBufferRef*/
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();



    CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    CGImageRef newImage2 = CGBitmapContextCreateImage(newContext);
    /*We release some components*/
    CGContextRelease(newContext);
    CGColorSpaceRelease(colorSpace);

    ;
    ;
    ;
    ;


    //UIImage *image= ;


    /*We relase the CGImageRef*/
    CGImageRelease(newImage2);

    //;

    /*We unlock theimage buffer*/
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);

    ;

}
</code></pre>

<p>效果很好..</p>

<p> <a href="http://crayoncoding.blogspot.com/2011/04/iphone-4-camera-views-at-once.html" rel="noreferrer noopener nofollow">http://crayoncoding.blogspot.com/2011/04/iphone-4-camera-views-at-once.html</a> </p>

<p>详细代码见以上链接</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - iPhone 相机访问权限?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/3558276/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/3558276/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - iPhone 相机访问权限?