GLKView와 UIImagePickerController를 모두 포함하는 스크린 샷을 캡처하려고 시도하기 전에 비슷한 문제가있었습니다. 때로는 검정색 화면이 표시되고 다른 경우에는 유효하지 않은 컨텍스트 (사용자와 유사한 코드를 사용하는 경우)에 대한 불만이 나타납니다. 솔루션을 찾을 수 없으므로 대신 AVFoundation 카메라를 구현하고 이후로 다시 보지 않았습니다. 다음은 몇 가지 간단한 소스 코드입니다.
가 ViewController.h
// Frameworks
#import <CoreVideo/CoreVideo.h>
#import <CoreMedia/CoreMedia.h>
#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>
@interface CameraViewController : UIViewController <AVCaptureVideoDataOutputSampleBufferDelegate>
// Camera
@property (strong, nonatomic) AVCaptureSession* captureSession;
@property (strong, nonatomic) AVCaptureVideoPreviewLayer* previewLayer;
@property (strong, nonatomic) UIImage* cameraImage;
@end
ViewController.m
#import "CameraViewController.h"
@implementation CameraViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupCamera];
}
- (void)setupCamera
{
AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] error:nil];
AVCaptureVideoDataOutput* output = [[AVCaptureVideoDataOutput alloc] init];
output.alwaysDiscardsLateVideoFrames = YES;
dispatch_queue_t queue;
queue = dispatch_queue_create("cameraQueue", NULL);
[output setSampleBufferDelegate:self queue:queue];
NSString* key = (NSString *) kCVPixelBufferPixelFormatTypeKey;
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
[output setVideoSettings:videoSettings];
self.captureSession = [[AVCaptureSession alloc] init];
[self.captureSession addInput:input];
[self.captureSession addOutput:output];
[self.captureSession setSessionPreset:AVCaptureSessionPresetPhoto];
self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
// CHECK FOR YOUR APP
self.previewLayer.frame = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width);
self.previewLayer.orientation = AVCaptureVideoOrientationLandscapeRight;
// CHECK FOR YOUR APP
[self.view.layer insertSublayer:self.previewLayer atIndex:0];
[self.captureSession startRunning];
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef newImage = CGBitmapContextCreateImage(newContext);
CGContextRelease(newContext);
CGColorSpaceRelease(colorSpace);
self.cameraImage = [UIImage imageWithCGImage:newImage];
CGImageRelease(newImage);
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
}
// Call whenever you need a snapshot
- (UIImage *)snapshot
{
NSLog(@"SNAPSHOT");
return self.cameraImage;
}
@end
이 코드는 선택된 프리 세트에 따라 입력 화상을 캡쳐 (이 경우에는 사진 : 852x640)이므로 보기와 함께 캡처하려면 다음 옵션을 권합니다.
- 캡처 후 이미지의 크기를 조정하고 자르고 번역하십시오. 장점 : 카메라가 여전히 부드럽게 작동합니다. 단점 : 추가 코드
- 대리인의 이미지를 업데이트하는
previewLayer
대신 UIImageView를 추가하십시오. 장점 : WYSIWYG. 단점 : 카메라의 실행 속도가 느려질 수 있습니다.
위의 두 경우 모두 스크린 샷을 찍은 후 결과 캡처를 다른 이미지와 병합해야합니다.
AVFoundation 및 관련 프레임 워크는 매우 어렵습니다. 따라서 이것은 사용자가 원하는 것을 얻는 데 매우 까다로운 구현입니다. 당신이 더 많은 정보를 원한다면 다음 예제를 확인하십시오 : 도움이
희망을!