好球員,我結束了使用AVFoundation,這到底是更好的解決方案,因爲它是定製的。我有self.fullScreenImageView
和self.previewLayer
作爲類屬性。你需要編寫自己的代碼來實現按鈕和攝影鏡頭的代碼。在這裏你可以玩照片的質量來獲得最佳的尺寸/質量。我用0.6,但你可以選擇任何你想要的。
@import MobileCoreServices;
@import AVFoundation;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view setBackgroundColor:[UIColor blackColor]];
self.fullScreenImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
self.fullScreenImageView.contentMode = UIViewContentModeScaleAspectFit;
[self.fullScreenImageView setCenter:self.view.center];
AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
captureSession.sessionPreset = AVCaptureSessionPresetHigh;
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (captureDevice) {
NSError *error;
AVCaptureDeviceInput *captureDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:captureDevice error:&error];
if (!error && [captureSession canAddInput:captureDeviceInput]) {
[captureSession addInput:captureDeviceInput];
}
}
AVCaptureStillImageOutput *stillImageOutput = [AVCaptureStillImageOutput new];
[stillImageOutput setOutputSettings:@{AVVideoCodecKey : AVVideoCodecJPEG,
AVVideoQualityKey : @(0.6)}];
[captureSession addOutput:stillImageOutput];
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:captureSession];
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
self.previewLayer.frame = self.fullScreenImageView.bounds;
self.previewLayer.position = CGPointMake(CGRectGetMidX(self.fullScreenImageView.bounds), CGRectGetMidY(self.fullScreenImageView.bounds));
[self.fullScreenImageView.layer addSublayer:self.previewLayer];
[captureSession startRunning];
CGRect circleRect = CGRectMake(0, (self.fullScreenImageView.bounds.size.height - self.fullScreenImageView.bounds.size.width)/2, self.fullScreenImageView.bounds.size.width, self.fullScreenImageView.bounds.size.width);
UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:circleRect];
CAShapeLayer *ringLayer = [CAShapeLayer layer];
ringLayer.path = circle.CGPath;
ringLayer.fillColor = nil;
ringLayer.strokeColor = [UIColor redColor].CGColor;
ringLayer.lineWidth = 2.0;
ringLayer.lineDashPattern = @[@5.0, @10.0];
[self.fullScreenImageView.layer addSublayer:ringLayer];
[self.navigationController setToolbarHidden:NO];
[self.navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque];
[self.navigationController.toolbar setTintColor:[UIColor whiteColor]];
// Add here some buttons, which are standard UIBarButtonItems
[self.view addSubview:self.fullScreenImageView];
}
設置AVCaptureSession並附加AVCaptureVideoPreviewLayer,然後您將完全控制攝像頭視圖的大小和位置。 – Sten
問題是,我只有攝像頭視圖,沒關係,但我也想擁有'UIImagePickerController'提供的所有功能。我只想知道**在哪裏**這個視圖控制器放置其相機視圖 –
AVCaptureSession擁有UIImagePickerController提供的一切以及更多。 UIImagePickerController旨在用作添加標準攝像機的簡單方法,而不是用於詳細控制。意志的佈局可能在不同的硬件上看起來不同。 – Sten