0

iPhone 画面全体のライブ画面キャプチャを実行する必要があります。画面には 3 つのコンテナ ビューが埋め込まれています。これらのコンテナーの 1 つが UIImagePickerController です。画面上のすべてが美しくキャプチャされますが、UIImagePickerController を持つ 1 つのコンテナーは黒です。操作の継続性がシームレスに見えるように、画面全体をキャプチャする必要があります。UIImagePickerController から現在画面に表示されているものをキャプチャする方法はありますか? 以下は、画面イメージをキャプチャするために使用しているコードです。

Apple の Technical Q&A QA1703も試しました。

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

助けてくれてありがとう!

4

1 に答える 1

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. の代わりに UIImageView を追加してpreviewLayer、デリゲートのイメージを更新しますcaptureOutput。長所: WYSIWYG。短所: カメラの動作が遅くなる可能性があります。

上記のどちらの場合でも、スクリーンショットを撮った後、結果のキャプチャを他の画像とマージする必要があります (思ったほど難しくはありません)。

AVFoundation とそれに関連するフレームワークは非常に難しいため、これは目的を達成するための非常に無駄のない実装です。詳細が必要な場合は、次の例を確認してください。

それが役立つことを願っています!

于 2012-12-30T09:50:07.083 に答える