2013-04-01 2 views
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]

ответ

2

Оверлей предназначен только для презентации в камере выбора.

Вам также необходимо объединить эти изображения в своем конечном UIImage (JPEG или TIFF или любом другом), который вы сохраняете на диск.

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

EDIT, вот код, который может помочь вам:

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

                }]; 


} 
+0

Проблема это захватить только одно изображение, что я использовал в этой строке [[само captureManager] captureStillImageWithOverlay: [UIImage imageNamed: @ "img2.png"]]; – Ram

+0

Внимательно прочитайте мой код и дайте ответ ... – Ram

+0

Хорошо, что я вижу в вашем коде, так это то, что вы вызываете «[[self captureManager] captureStillImageWithOverlay: [UIImage imageNamed: @" img2.png "]];' и внутри этого метода он объединяет «img2.png» с тем, что приходит через «videoConnection». Разве это не то, что вы хотели? –

Смежные вопросы