【问题标题】:Couldn't capture overlay image无法捕获叠加图像
【发布时间】:2013-04-01 06:25:49
【问题描述】:

我有 3 张图片。我一一加载到相机叠加图像。然后我需要拍摄快照。但是当我单击按钮时,它会拍摄特定图像的快照。当我将 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];

                                                     }];


} 

我从这个网址引用[http://www.musicalgeometry.com/?p=1681]

【问题讨论】:

    标签: iphone camera overlay avfoundation avcapturesession


    【解决方案1】:

    叠加层仅用于在相机选择器中展示。

    您还需要在保存到磁盘的最终 UIImage(JPEG 或 TIFF 或其他)中合并这些图像。

    Other people have had (and have solved) this same problem as you.

    编辑,这里有一些代码可以帮助你:

    - (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];
    
                                                         }];
    
    
    } 
    

    【讨论】:

    • 问题是它只捕获我在这一行中使用的一张图像 [[self captureManager] captureStillImageWithOverlay:[UIImage imageNamed:@"img2.png"]];
    • 好吧,我在您的代码中看到的是您正在调用“[[self captureManager] captureStillImageWithOverlay:[UIImage imageNamed:@"img2.png"]];”,并且在该方法中,它将“img2.png”与通过“videoConnection”传入的任何内容相结合。这不是你想要的吗?
    • 现在我想在相机叠加层中呈现时捕获 img1、img3...但是当我使用这一行时 [[self captureManager] captureStillImageWithOverlay:[UIImage imageNamed:@"img2.png"]];它只捕获 img2。
    • hmmmm...也许如果您将参数更改为“img1.png”或“img3.png”,您可能会在其中获得 img1 或 img3,是吗?
    • 我是 iOS 的初学者。帮帮我
    猜你喜欢
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    相关资源
    最近更新 更多