How to Programmatically Take Photos While Recording Video

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 07:52:17

Actually you can do it with any device that can record video:

  • Create and configure a AVCaptureVideoDataOutput.
  • Add it to your AVCaptureSession.
  • Add a AVCaptureVideoDataOutputSampleBufferDelegate.
  • Implement captureOutput:didOutputSampleBuffer:fromConnection: in the delegate and get the image with imageFromSampleBuffer:.

Some similar code can be found here, where images are captured at a given interval, but you only want one image.

In iOS7 there has a new api to capture UIView, we can get image and do something.

eg:

UIView+Screenshot.h
-(UIImage *)convertViewToImage;

UIView+Screenshot.m

-(UIImage *)convertViewToImage
{
    UIGraphicsBeginImageContext(self.bounds.size);
    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

Apple's Document: drawViewHierarchyInRect:afterScreenUpdates:

Ron

Through AVCaptureSession you can easily achieve iPhone 5S in the camera app functionality. In your view touch event:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection "

You can get current frame on above delegate method and save it in your gallery.

Please go through below link:

https://developer.apple.com/library/ios/samplecode/RosyWriter/Introduction/Intro.html

Mingebag

I agree with Rivera's answer ! You are going to need AV

And this is how I do it after the hole AV stuff

CGImageRef bigImage = image.CGImage;
CGRect rectImage = CGRectMake(self.scannArea.origin.x*widhtRatio,
                              self.scannArea.origin.y*heightRatio - 50,
                              self.scannArea.size.width*widhtRatio,
                              self.scannArea.size.height*heightRatio);
CGImageRef part = CGImageCreateWithImageInRect(bigImage, rectImage);
UIImageWriteToSavedPhotosAlbum([UIImage imageWithCGImage:part], nil, nil, nil);

image = [self drawImage:[UIImage imageNamed:@"overlaygraphic.png"] 
                inImage:image 
                atPoint:CGPointMake(self.scannArea.origin.x*widhtRatio, 
                                    self.scannArea.origin.y*heightRatio - 50)];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

if you want to see the hole sample np. I will upload my class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!