0

オーバーレイ画像で撮りたいです。

コード:

- (void)addStillImageOutput
{

   // NSLog(@"You capture image");
    [self setStillImageOutput:[[[AVCaptureStillImageOutput alloc] init] autorelease]];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil];
    [[self stillImageOutput] setOutputSettings:outputSettings];

    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
        for (AVCaptureInputPort *port in [connection inputPorts]) {
            if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) {
            break;
        }
    }

    [[self captureSession] addOutput:[self stillImageOutput]];
}

- (void)captureStillImage
{
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
        for (AVCaptureInputPort *port in [connection inputPorts]) {
            if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) {
            break;
        }
    }

    NSLog(@"about to request a capture from: %@", [self stillImageOutput]);


    [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
                                                         completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
                                                             CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
                                                             if (exifAttachments) {
                                                                 NSLog(@"attachements: %@", exifAttachments);
                                                             } else {
                                                                 NSLog(@"no attachments");
                                                             }
                                                             NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
                                                             UIImage *image = [[UIImage alloc] initWithData:imageData];
                                                             [self setStillImage:image];
                                                             [image release];
                                                             [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];
                                                         }];


}

OverlayImage:

-(void)ButtonPressed1{

UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sof.png"]];
    [overlayImageView setFrame:CGRectMake(30, 100, 260, 200)];


    [[self view] addSubview:overlayImageView];
    [overlayImageView release];
}
4

1 に答える 1

2

captureStillImageAsynchronouslyFromConnection接続()からのデータのみをキャプチャAVCaptureConnectionし、追加の画像は接続されていません。彼らは視界にいます。

したがって、すべての要素(画像とオーバーレイ)を含む画像を「生成」するには、次のような操作を行う必要があります。

UIGraphicsBeginImageContext(stillImage.size);

[stillImage drawInRect:CGRectMake(0, 0, picture.size.width, picture.size.height)];
[overlayImageView drawInRect:CGRectMake(overlayImageView.frame.origin.x, overlayImageView.frame.origin.y, overlayImageView.frame.size.width, overlayImageView.frame.size.height)];

UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

このコードは、画像サイズが画面サイズと等しいことを考慮しています。画像サイズが異なる場合は、メソッドにオーバーレイを配置するための座標を計算する必要がありますdrawRect。英語が下手でごめんなさい。

于 2013-03-19T16:55:14.533 に答える