How to set frame size using Avcapture

I am using AVCaptureSession for shooting. Here is my code:

 AVCaptureSession * newSession = [[AVCaptureSession alloc] init]; // Configure our capturesession newSession.sessionPreset = AVCaptureSessionPresetMedium; 

I have an image with a size of 360 * 480. But here's my problem, I want an image with exactly the size of 280 * 200. I'm not interested in cropping the image. Any way to set the image size taken by AVCaptureSession ? Thanks in advance.

+1
ios ios4 ios5
Jan 14 '13 at 9:27
source share
1 answer

With AVCapturePresets, you have an explicit size by which videos or photos will be taken. After you take a picture, you can manipulate (crop, resize) according to your desired size, but you cannot set a predefined capture size.

There are acceptable presets:

 NSString *const AVCaptureSessionPresetPhoto; NSString *const AVCaptureSessionPresetHigh; NSString *const AVCaptureSessionPresetMedium; NSString *const AVCaptureSessionPresetLow; NSString *const AVCaptureSessionPreset320x240; NSString *const AVCaptureSessionPreset352x288; NSString *const AVCaptureSessionPreset640x480; NSString *const AVCaptureSessionPreset960x540; NSString *const AVCaptureSessionPreset1280x720; 

source: https://developer.apple.com/library/mac/#documentation/AVFoundation/Reference/AVCaptureSession_Class/Reference/Reference.html

This method is what I use, which should help you scale to the right size:

 -(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize; { UIGraphicsBeginImageContext( newSize ); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 

Initializing a session and setting up a preset:

 // create a capture session if(session == nil){ session = [[AVCaptureSession alloc] init]; } if ([session canSetSessionPreset:AVCaptureSessionPreset320x240]) { session.sessionPreset = AVCaptureSessionPreset320x240; } else { NSLog(@"Cannot set session preset"); } 
+3
Jan 14 '13 at 16:23
source share



All Articles