0

我想用overlay圖像拍照。使用AVFoundation框架捕獲overlayimage

代碼:

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

回答

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方法中。對不起英語不好。

+0

我可以在哪裏添加此代碼? – Ram 2013-03-20 03:34:34

+0

您可以添加觀察者'[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(processImage)name:kImageCapturedSuccessfully object:nil];'on - (void)viewDidLoad'並且在 - (void)processImage中添加上面的代碼。 – djserva 2013-03-20 13:54:14

+0

我需要用新移動的位置重疊圖像。我設置了drawInRect框架。但是當我將圖像從默認位置移動到某個其他位置時,那麼我想用新移動的位置與覆蓋圖像拍照...但是我只有默認的幀尺寸覆蓋圖像。我如何獲得移動位置框架? – Ram 2013-03-25 11:24:21