2012-12-30 4 views
0

iPhone 화면 전체를 실시간 화면 캡처해야합니다. 화면에는 3 개의 컨테이너 뷰가 임베드되어 있습니다. 이러한 컨테이너 중 하나는 UIImagePickerController입니다. 화면의 모든 것이 아름답게 캡처되지만 UIImagePickerController가있는 컨테이너는 검은 색입니다. 전체 화면 캡처가 필요하므로 작업의 연속성이 매끄럽게 보입니다. 현재 UIImagePickerController에서 화면에 표시된 내용을 캡처하는 방법이 있습니까? 다음은 화면 이미지를 캡처하는 데 사용하는 코드입니다.비디오 모드의 UIImagePicker가있는 임베디드 컨트롤러가 3 개인 화면 캡처?

나는 또한 Apple's Technical Q&A QA1703을 시도했다.

UIGraphicsBeginImageContextWithOptions(myView.bounds.size, YES, 0.0f); 
[myView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

미리 도움을 청하십시오.

답변

1

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)이므로 보기와 함께 캡처하려면 다음 옵션을 권합니다.

  1. 캡처 후 이미지의 크기를 조정하고 자르고 번역하십시오. 장점 : 카메라가 여전히 부드럽게 작동합니다. 단점 : 추가 코드
  2. 대리인의 이미지를 업데이트하는 previewLayer 대신 UIImageView를 추가하십시오. 장점 : WYSIWYG. 단점 : 카메라의 실행 속도가 느려질 수 있습니다.

위의 두 경우 모두 스크린 샷을 찍은 후 결과 캡처를 다른 이미지와 병합해야합니다.

AVFoundation 및 관련 프레임 워크는 매우 어렵습니다. 따라서 이것은 사용자가 원하는 것을 얻는 데 매우 까다로운 구현입니다. 당신이 더 많은 정보를 원한다면 다음 예제를 확인하십시오 : 도움이

희망을!