iOS robienie zdjęć programowo

Wiem, że to możliwe, widziałem to w niektórych aplikacjach (iGotYa jest moim zdaniem najbardziej znany). Wiem, jak ustawić wszystko do robienia zdjęć, zapisywania i w ogóle. Ale jak można to zrobić programowo? wystarczy, że użytkownik kliknie jakiś przycisk (w zwykłym kontrolerze widoku) i automatycznie zrobi zdjęcie za pomocą przedniej kamery i zapisze je (lub nie, po prostu dostając je jako UIImage)

Dzięki!

Author: kaiz.net, 2012-03-14

5 answers

Jest to bardzo proste, wystarczy użyć AVFoundation reference guide:

Https://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html

Jeśli nie chcesz, aby użytkownik widział wejście podglądu, możesz po prostu pominąć ustawioną warstwę podglądu część kodu.

Edit: aby być bardziej szczegółowym.

1) konfigurację przechwytywania ustawiasz za pomocą AVFoundation.

  • Ustaw wejście kamery na czołowe, wyłącz lampę błyskową itp itp.

2) pomijasz część, w której ustawiona jest warstwa podglądu wideo.

3) wywołujesz metodę captureStillImageAsynchronouslyFromconnection:completionHandler: Gdy chcesz, aby zdjęcie zostało zrobione.

Uwaga: jeśli chcesz, aby flash nie był słyszalny i taki, możesz naruszać prawa użytkownika w niektórych krajach (na przykład w Japonii). Jednym z obejścia, które znam, jest przechwytywanie klatki wideo (nie uruchamia flash).

 22
Author: Pochi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-14 02:17:35

Można to również zrobić bez AVFoundation i jest to moim zdaniem łatwiejszy sposób na implementację go przy użyciu tylko UIImagePickerController. Istnieją 3 Warunki:

  1. oczywiście urządzenie musi mieć aparat
  2. musisz ukryć kontrolki kamery
  3. Następnie po prostu użyj metody Takepicture z UIImagePickerController

Poniżej znajduje się prosty przykład, który zazwyczaj wyzwalasz po naciśnięciu przycisku

- (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];
                     }];
}
 15
Author: GrandSteph,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-12 07:36:13

VLBCameraView jest biblioteką, która używa AVFoundation do robienia zdjęć.

Podgląd jest wyświetlany w widoku, który można następnie wywołać metodę VLBCameraView#takepicture programowo zrobić zdjęcie.

Pochodzi z CocoaPods.

 2
Author: samwize,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-26 04:34:38

W .plik h

@interface ABCViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *imageView;

- (IBAction)takePhoto:  (UIButton *)sender;
- (IBAction)selectPhoto:(UIButton *)sender;

@end

W .plik m

@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];

}
 1
Author: Nischal Hada,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-11 22:10:18

Oto kod do custom Camera Objective-C. Możesz dodać funkcje, przyciski zgodnie z życzeniem.

#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
 0
Author: varender singh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-20 12:50:19