我知道這是可能的,在一些應用程序中看到了這一點(iGotYa是我相信最有名的)。 我知道如何設置拍照,保存照片等一切。 但是它如何以編程方式完成?只需讓用戶點擊一下按鈕(在普通的視圖控制器中),它就會自動拍攝一張使用前置攝像頭的照片並保存(不然,只需將其作爲UIImage)iOS以編程方式拍照
謝謝!
我知道這是可能的,在一些應用程序中看到了這一點(iGotYa是我相信最有名的)。 我知道如何設置拍照,保存照片等一切。 但是它如何以編程方式完成?只需讓用戶點擊一下按鈕(在普通的視圖控制器中),它就會自動拍攝一張使用前置攝像頭的照片並保存(不然,只需將其作爲UIImage)iOS以編程方式拍照
謝謝!
這個很簡單,只需使用AVFoundation參考指南:
如果不希望用戶看到預覽輸入你可以跳過的設定預覽層的一部分代碼。
編輯:更詳細。
1)您使用AVFoundation設置捕捉配置。
2)跳過該視頻預覽層設定的部分。
3)只要你想拍照,你可以調用captureStillImageAsynchronouslyFromConnection:completionHandler:方法。
注意:如果您不想聽到閃光燈,那麼您可能會侵犯某些國家(例如日本)的用戶權利。我知道這樣做的一種解決方法是捕獲視頻幀(不觸發閃光)。
爲什麼拍攝的圖像是黑色的?任何想法? – 2016-06-20 11:25:30
該應用是否有權使用相機? (我認爲它現在需要,但不知道) – Pochi 2016-06-21 02:47:37
是。應用程序有權限。 – 2016-06-21 05:27:26
你也可以在沒有AVFoundation的情況下做到這一點,在我看來,只用UIImagePickerController就可以實現它。 有3個條件:
下面是一個簡單例如,你通常會在按下按鈕後觸發
- (IBAction)takePhoto:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
picker.showsCameraControls = NO;
[self presentViewController:picker animated:YES
completion:^ {
[picker takePicture];
}];
}
也'self'應該採用'UINavigationControllerDelegate'和'UIImagePickerControllerDelegate'協議。 – gdros 2016-01-20 15:10:09
拍攝照片後,如何在不點擊UsePhoto的情況下移動另一個VC? – 2016-12-19 09:27:48
在.h文件中
@interface ABCViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)takePhoto: (UIButton *)sender;
- (IBAction)selectPhoto:(UIButton *)sender;
@end
In。米文件
@interface ABCViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
- (IBAction)takePhoto:(UIButton *)sender {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Device has no camera"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[myAlertView show];
} else {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
}
- (IBAction)selectPhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
這裏是目的-C定製的相機的代碼。您可以根據您的意願添加功能, 按鈕。的
#import "CustomCameraVC.h"
@interface CustomCameraVC() {
BOOL frontCamera;
}
@property (strong,nonatomic) AVCaptureSession *captureSession;
@property (strong,nonatomic) AVCaptureStillImageOutput *stillImageOutput;
@property (strong,nonatomic) AVCaptureVideoPreviewLayer *videoPreviewLayer;
@property (weak, nonatomic) IBOutlet UIView *viewCamera;
@end
@implementation CustomCameraVC
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
frontCamera = NO;
[self showCameraWithFrontCamera:frontCamera];
}
-(void)showCameraWithFrontCamera:(BOOL)flag {
self.captureSession = [[AVCaptureSession alloc]init];
self.captureSession.sessionPreset = AVCaptureSessionPresetPhoto;
AVCaptureDevice *captureDevice;
if(flag) {
captureDevice= [self frontCamera];
}
else {
captureDevice= [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
[self.captureSession addInput:input];
self.stillImageOutput = [AVCaptureStillImageOutput new];
self.stillImageOutput.outputSettings = @{AVVideoCodecKey:AVVideoCodecJPEG};
[self.captureSession addOutput:_stillImageOutput];
self.videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
self.videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.videoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortrait;
[self.viewCamera.layer addSublayer:self.videoPreviewLayer];
[self.captureSession startRunning];
self.videoPreviewLayer.frame = self.viewCamera.bounds;
}
- (AVCaptureDevice *)frontCamera {
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if ([device position] == AVCaptureDevicePositionFront) {
return device;
}
}
return nil;
}
- (IBAction)btnCaptureImagePressed:(id)sender {
AVCaptureConnection * videoConnection = [_stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
[_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef _Nullable sampleBuffer, NSError * _Nullable error) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:sampleBuffer];
UIImage *image = [[UIImage alloc]initWithData: imageData];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}];
}
@end
可能重複的[iOS的自動拍攝](http://stackoverflow.com/questions/5237941/ios-take-picture) – bryanmac 2012-03-14 02:00:47
'UIImagePickerController'的'takePicture'方法是一種方法。 – FluffulousChimp 2012-03-14 02:06:19