我正在研究将前置摄像头视频提要显示到类似于FaceTime的UIView中。我知道可以使用AVCaptureVideoPreviewLayer轻松完成此操作。有没有使用AVCaptureVideoPreviewLayer的另一种方法?
这仅出于教育目的。
更新:
我发现这可以通过UIImagePickerController完成
UIImagePickerController *cameraView = [[UIImagePickerController alloc] init];
cameraView.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraView.showsCameraControls = NO;
[self.view addSubview:cameraView.view];
[cameraView viewWillAppear:YES];
[cameraView viewDidAppear:YES];
最佳答案
如果尝试操纵像素,则可以将以下方法放在要作为委托分配给AVCaptureVideoDataOutputSampleBufferDelegate的类中:
-(void) captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
CVImageBufferRef pb = CMSampleBufferGetImageBuffer(sampleBuffer);
if(CVPixelBufferLockBaseAddress(pb, 0)) //zero is success
NSLog(@"Error");
size_t bufferHeight = CVPixelBufferGetHeight(pb);
size_t bufferWidth = CVPixelBufferGetWidth(pb);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(pb);
unsigned char* rowBase= CVPixelBufferGetBaseAddress(pb);
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL)
NSLog(@"Error");
// Create a bitmap graphics context with the sample buffer data.
CGContextRef context= CGBitmapContextCreate(rowBase,bufferWidth,bufferHeight, 8,bytesPerRow, colorSpace, kCGImageAlphaNone);
// Create a Quartz image from the pixel data in the bitmap graphics context
CGImageRef quartzImage = CGBitmapContextCreateImage(context);
UIImage *currentImage=[UIImage imageWithCGImage:quartzImage];
// Free up the context and color space
CFRelease(quartzImage);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
if(CVPixelBufferUnlockBaseAddress(pb, 0 )) //zero is success
NSLog(@"Error");
}
然后将该图像连接到View控制器中的UIImageView。
查找kCGImageAlphaNone标志。这将取决于您在做什么。
关于iphone - 在UIView中显示相机供稿,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14902781/