0

3枚の画像があります。カメラのオーバーレイ画像に 1 つずつ読み込みます。次に、スナップショットを撮る必要があります。しかし、ボタンをクリックすると、特定の画像のスナップショットが撮られます。imageName.png を captureStillImageWithOverlay に入れると、その画像のスナップショットのみが取得され、オーバーレイに存在する場合は他の画像は取得されません

ボタンクリックコード:

- (void)ButtonPressed {

  [[self captureManager] captureStillImageWithOverlay:[UIImage imageNamed:@"img2.png"]];

  }

画像をロードする:

-(void)loadNextPage:(int)index
{


    int countFlag=0;
    for(int i=index*4;i<(index+1)*4;i++)
    {
        UIButton *imageView=[[UIButton alloc]initWithFrame:CGRectMake((320*index)+countFlag*80+ 2, 5, 75, 75)];
        imageView.tag=i+1;
        [imageView addTarget:self action:@selector(imageViewClicked:) forControlEvents:UIControlEventTouchUpInside];
        [imageView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
        [imageView.layer setBorderWidth:1.0f];
        switch ((i+1)%5) {
            case 0:
                [imageView setImage:[UIImage imageNamed:@"img1.png"] forState:UIControlStateNormal];

                break;

            case 1:
                [imageView setImage:[UIImage imageNamed:@"img2.png"]  forState:UIControlStateNormal];

                break;

            case 2:
                [imageView setImage:[UIImage imageNamed:@"img3.png"]  forState:UIControlStateNormal];
                break;

}

        [myScrollView addSubview:imageView];



        [imageView release];
        countFlag++;
    }
}

オーバーレイ画像をキャプチャ:

- (void)captureStillImageWithOverlay:(UIImage*)overlay
{
    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];

                                                             CGSize imageSize = [image size];
                                                             CGSize overlaySize = [overlay size];

                                                             UIGraphicsBeginImageContext(imageSize);

                                                             [image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];

                                                             CGFloat xScaleFactor = imageSize.width / 320;
                                                             CGFloat yScaleFactor = imageSize.height / 480;

                                                             [overlay drawInRect:CGRectMake(30 * xScaleFactor, 100 * yScaleFactor, overlaySize.width * xScaleFactor, overlaySize.height * yScaleFactor)]; // rect used in AROverlayViewController was (30,100,260,200)

                                                             UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();

                                                             [self setStillImage:combinedImage];

                                                             UIGraphicsEndImageContext();

                                                             [image release];
                                                             [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];

                                                     }];


} 

この URL [http://www.musicalgeometry.com/?p=1681]から参照します。

4

1 に答える 1

2

オーバーレイは、カメラ ピッカーで表示するためのものです。

また、ディスクに保存する最終的な UIImage (JPEG や TIFF など) でこれらの画像を組み合わせる必要があります。

他の人があなたと同じ問題を抱えていました (そして解決しました)

編集、これがあなたを助けるかもしれないいくつかのコードです:

- (void)captureStillImageWithOverlay:(NSArray *) arrayOfImageFiles
{
    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];

                                                             CGSize imageSize = [image size];

                                                             UIGraphicsBeginImageContext(imageSize);

                                                             [image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];

                                                             CGFloat xScaleFactor = imageSize.width / 320;
                                                             CGFloat yScaleFactor = imageSize.height / 480;

                                                             for(NSString * imageFileName in arrayOfImageFiles)
                                                             {
                                                                 // images named @"img1" or @"img1.png" should work
                                                                 UIImage * overlay = [UIImage imageNamed: imageFileName];
                                                                 if(overlay)
                                                                 {
                                                                     CGSize overlaySize = [overlay size];

                                                                     [overlay drawInRect:CGRectMake(30 * xScaleFactor, 100 * yScaleFactor, overlaySize.width * xScaleFactor, overlaySize.height * yScaleFactor)]; // rect used in AROverlayViewController was (30,100,260,200)
                                                                 } else {
                                                                     NSLog( @"could not find an image named %@", imageFileName);
                                                                 }
                                                             }

                                                             UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();

                                                             [self setStillImage:combinedImage];

                                                             UIGraphicsEndImageContext();

                                                             [image release];
                                                             [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];

                                                     }];


} 
于 2013-04-01T06:27:39.967 に答える