Capturing video through Avfoundation
Capturing a camera's static shot in the iOS 6 SDK (iOS 5 SDK or above), making a similar "video recording at the same time" can result in many ways, such as through
Uiimagepickercontroller, call the API provided by the IOS SDK to capture the screen, or
Avfoundation, the avcapturesession of the image is established, and the corresponding Input and Output are set. The method used in this article belongs to the latter, and we use avcapturestillimageoutput as the output end of the avcapturesession, outputting the static image.
Before you begin, add avfoundation.framework to your project and reference the header file in the corresponding category.
//----------------------------
Instantiate a video Capture object [Build Avcapturesession]
//----------------------------
Avcapturesession *session = [[Avcapturesession alloc]init];
Setting quality
Session.sessionpreset = Avcapturesessionpresetmedium;
//----------------------------
Create a video layer
//----------------------------
Avcapturevideopreviewlayer *capturevideopreviewlayer = [[Avcapturevideopreviewlayer alloc]initWithSession:session] ;
Capturevideopreviewlayer.frame = CGRectMake (0, 0, 320, 568);
Set the zoom ratio
CGFloat camscaleup = 1.8;
Capturevideopreviewlayer.affinetransform = Cgaffinetransformmakescale (Camscaleup, camScaleup);
[Self.view.layer Addsublayer:capturevideopreviewlayer];
//----------------------------
Capturing video
//----------------------------
Specify the media type to get the default capture device Avcapturedevice object
Avcapturedevice *device = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];
Nserror *error = nil;
Instantiating a capture input object
Avcapturedeviceinput *input = [Avcapturedeviceinput deviceinputwithdevice:device error:&error];
if (!input) {
NSLog (@ "%@", error);
}
Join the capture session
[Session Addinput:input];
//----------------------------
Set the output image
//----------------------------
Instantiating a static image output object
Avcapturestillimageoutput *stillimageoutput = [[Avcapturestillimageoutput alloc]init];
Setting output parameters
Nsdictionary *dict = [[Nsdictionary alloc]initwithobjectsandkeys:avvideocodecjpeg,avvideocodeckey, Nil];
Stillimageoutput.outputsettings = Dict;
[Session Addoutput:stillimageoutput];
[Session startrunning];
//----------------------------
//
//----------------------------
//----------------------------
//
//----------------------------
//----------------------------
//
//----------------------------
//----------------------------
//
//----------------------------
/*!
Avcapturesession presets are available for high-resolution photo quality output.
Clients may set a avcapturesession instance ' s Sessionpreset to Avcapturesessionpresetphoto
For full resolution photo quality output.
*/
Avf_export nsstring *const Avcapturesessionpresetphoto ns_available (10_7, 4_0);
/*!
The Avcapturesession presets are designed for high quality video and audio output.
Avcapturesessionpresethigh is the default value for Sessionpreset.
*/
Avf_export nsstring *const Avcapturesessionpresethigh ns_available (10_7, 4_0);
/*!
The avcapturesession preset is suitable for medium-quality output.
The output that is implemented is suitable for video and audio bitrate sharing in wireless networks.
*/
Avf_export nsstring *const avcapturesessionpresetmedium ns_available (10_7, 4_0);
/*!
The avcapturesession preset is suitable for low-quality outputs.
In order to achieve the output video and audio bitrate suitable for sharing 3G.
*/
Avf_export nsstring *const avcapturesessionpresetlow ns_available (10_7, 4_0);
The following is not for ipad iphone. For Mac_os
//-----------------------------------------------
/*!
Use this constant to set the time for the Optimize animation BeginTime property to set to 0.
A constant is a small, non-zero, positive value that avoids optimization
*/
Avf_export Const Cftimeinterval Avcoreanimationbegintimeatzero ns_available (10_7, 4_0);
/*!
Preserves the aspect ratio and fits within the layer range.
*/
Avf_export nsstring *const avlayervideogravityresizeaspect ns_available (10_7, 4_0);
/*!
Preserves the aspect ratio; fills the layer boundaries.
*/
Avf_export nsstring *const Avlayervideogravityresizeaspectfill ns_available (10_7, 4_0);
/*!
Stretches to fill the layer boundaries.
*/
Avf_export nsstring *const avlayervideogravityresize ns_available (10_7, 4_0);
Study the contents of the IOS camera zoom