0

我有 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]

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 回答