iPhone 相机访问权限?

2024-04-29

我想知道如何访问 iPhone 相机并实时使用它:例如,仅在相机视图上绘图。

另一个相关问题:

可以显示吗同时 4 个摄像机视图就像 Mac 上的“Photo Booth”一样。


您可以使用 AVFoundation 来做到这一点

- (void)initCapture {

    AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput 
                                          deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] 
                                          error:nil];

    AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];

    captureOutput.alwaysDiscardsLateVideoFrames = YES; 

    dispatch_queue_t queue;
    queue = dispatch_queue_create("cameraQueue", NULL);
    [captureOutput setSampleBufferDelegate:self queue:queue];
    dispatch_release(queue);

    NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey; 
    NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]; 
    NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key]; 
    [captureOutput setVideoSettings:videoSettings]; 


    self.captureSession = [[AVCaptureSession alloc] init];
    [self.captureSession setSessionPreset:AVCaptureSessionPresetLow];

    [self.captureSession addInput:captureInput];
    [self.captureSession addOutput:captureOutput];

    [self.captureSession startRunning];

    self.customLayer = [CALayer layer];

    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.view.layer addSublayer:imageView.layer];
    //self.customLayer.frame =CGRectMake(0, 0, 200,150);
    //self.customLayer.contentsGravity = kCAGravityResizeAspectFill;

    [self.view.layer insertSublayer:self.customLayer atIndex:4];
    //[self.view.layer addSublayer:self.customLayer];


    self.customLayer1 = [CALayer layer];
    //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.view.layer addSublayer:self.customLayer1];




    self.customLayer2 = [CALayer layer];
    //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.view.layer addSublayer:self.customLayer2];


    self.customLayer3 = [CALayer layer];
    //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;
    [self.view.layer addSublayer:self.customLayer3];



}



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


    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] 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);

    [self.customLayer performSelectorOnMainThread:@selector(setContents:) withObject: (id) newImage2 waitUntilDone:YES];
    [self.customLayer1 performSelectorOnMainThread:@selector(setContents:) withObject: (id) newImage2 waitUntilDone:YES];
    [self.customLayer2 performSelectorOnMainThread:@selector(setContents:) withObject: (id) newImage2 waitUntilDone:YES];
    [self.customLayer3 performSelectorOnMainThread:@selector(setContents:) withObject: (id) newImage2 waitUntilDone:YES];


    //  UIImage *image= [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationRight];


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

    //  [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];

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

    [pool drain];

} 

效果很好..

http://crayoncoding.blogspot.com/2011/04/iphone-4-camera-views-at-once.html http://crayoncoding.blogspot.com/2011/04/iphone-4-camera-views-at-once.html

详细代码见上面链接

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iPhone 相机访问权限? 的相关文章

  • 启动使用 Simperium 的应用程序时 objectFromJSONString 崩溃

    我得到了一个JSON当我尝试启动使用 Simperium 框架的应用程序时崩溃 NSCFString objectFromJSONString unrecognized selector sent to instance 0x6c561a0
  • 了解 React Native 中的默认字体大小

    在过去的几个月里 我一直在开发一个 React Native 应用程序 但有些事情总是让我困惑 而我现在正试图弄清楚它的真相 我正在尝试标准化应用程序中的字体大小 正文 标题等 并且正在努力了解 React Native 究竟从哪里获取默认
  • 从基元创建自定义形状

    我正在尝试通过组合原始形状来创建自定义物理形状 目标是创建一个圆形立方体 合适的方法似乎是初始化 形状 变换 我在这里找到的https developer apple com library prerelease ios documenta
  • 如何从 Xib 文件加载自定义 UITableViewCell?

    问题很简单 如何加载自定义UITableViewCell来自 Xib 文件 这样做可以让您使用 Interface Builder 来设计您的单元 由于内存管理问题 答案显然并不简单 这个线程 https stackoverflow com
  • 使用 iPhone 控制蓝牙音频设备

    我正在寻找为 iPhone 编写应用程序 它将能够控制汽车中的收音机和 CD 播放器 收音机和播放器具有可用的蓝牙连接 我开始这个问题是为了获得这个地方所需的所有信息 我有几个问题 但如果您发现任何我没有要求的对我开始开发此应用程序不重要的
  • 将 Xcode 4.5 新 XIB 文件恢复到 iOS<6

    我已经安装了Xcode 4 5 with iOS6 SDK以及其他用于测试目的的旧 SDK 从 4 3 到 6 0 很美 但是有一个BIG问题 生成一个新的 XIB 文件以兼容 iOS6 这是一个问题 因为我的应用程序需要运行在旧设备 不只
  • iPhone SDK:将 UIActivityIndi​​catorView 添加到 UITableViewCell

    为什么单元格在这段代码中没有显示任何内容 UIActivityIndicatorView spinner UIActivityIndicatorView alloc initWithActivityIndicatorStyle UIActi
  • 如何在 Safari 上打开本地 html 文件?

    我想打开本地 html 文件Safari集成到我的Swift 3应用 我知道如何使用网址来做到这一点 这是我用来执行此操作的代码 let encodedString url addingPercentEncoding withAllowed
  • 如何通过我的 ios 应用程序的指示打开苹果地图应用程序

    我的目标是从 ios 应用程序打开带有方向的地图应用程序 我可以打开地图应用程序 但它没有显示方向 我编写的代码如下 NSString mystr NSString alloc initWithFormat http maps apple
  • Mapkit 在 IOS 13 中使用过多的 CPU

    最近 在一些用户更新到 iOS 13 x 后 我的 iOS 应用程序开始频繁崩溃 在 iOS 12 x 中没有出现该问题 我正在使用 Mapkit 渲染一些 MKPolygons 和 MKPolylines MKPolylines 被删除并
  • 将 UIRefreshControl 用于 UIWebView

    我在 iOS 6 中看到了 UIRefreshControl 我的问题是是否可以通过下拉来刷新 WebView 而不是像在邮件中那样让它弹出 我使用 rabih 的代码是 WebView UIRefreshControl refreshCo
  • 如何检测 iOS 8 上的包含应用程序是否启用了应用程序扩展?

    我正在 iOS 8 beta 上开发一个自定义键盘 我想告诉用户如果我的自定义键盘未启用 如何在包含应用程序中启用它 有什么方法可以检测应用程序扩展是否已启用 首先让我们设置一些常量 以便于彼此理解 包含应用程序 安装扩展并保存扩展二进制文
  • 如何在 swift 中以编程方式使用坐标打开地图应用程序?

    我想在地图应用程序中打开纬度和经度 我尝试了这段代码HERE https stackoverflow com questions 12504294 programmatically open maps app in ios 6 func g
  • ExpandableLabel iOS 中的“少看”

    我正在使用第三方库可扩展标签 https github com apploft ExpandableLabel实施一个see more特征 我正在寻找仅快速的解决方案 其中包含标签中的文本而不是按钮中的文本 因此这可以完美地工作 添加库并更
  • 如何从 UILabel 创建图像?

    我目前正在 iPhone 上开发一个简单的类似 Photoshop 的应用程序 当我想要展平图层时 标签位于良好的位置 但字体大小不佳 这是我要展平的代码 UIGraphicsBeginImageContext CGSizeMake wid
  • iOS:我如何知道某个属性是否符合 KVO 标准?

    In the 键值观察编程指南 https developer apple com library archive documentation Cocoa Conceptual KeyValueObserving KeyValueObser
  • 调用了 numberOfRowsInSection 但未调用 cellForRowAtIndexPath

    在我的表视图中节中的行数被调用两次但是cellForRowAtIndexPath不叫 我想在 tableView 中显示 Facebook 好友列表 如果 cellForRowAtIndexPath 调用我的问题就解决了 我在这里的数组中得
  • 使用 NSFileHandle 覆盖数据

    使用 NSFileHandle 使用 truncateFileAtOffset 从文件末尾删除 n 个字符非常容易 void removeCharacters int numberOfCharacters fromEndOfFile NSF
  • 在iOS中设置框架的原点

    我正在尝试以编程方式设置框架的原点 Method1 button frame origin y 100 方法二 CGRect frame button frame frame origin y 100 我尝试了方法 1 但它不起作用 显示错
  • iOS 7 导航栏颜色在 iPhone 4 上无法正确显示

    我的导航栏颜色在 iOS 7 中正常显示 部署iOS 6 0 但如果系统版本是iOS 7 0或更高版本 部分导航栏颜色在iPhone 4上无法正常显示 在iPhone 5上工作正常 我是这样做的 if SYSTEM VERSION GREA

随机推荐