在iOS 10.1中,相机功能崩溃。但它在iOS 9.3及以下版本中运行良好

时间:2017-03-21 06:44:24

标签: ios objective-c ios10

我是目标c和iOS的新手。  在我的项目中,我编写了相机代码,它在iOS 9.3及更低版本中运行良好。但它在iOS 10.1中崩溃了。

我已经像这样创建了previewView

<app-loader *ngIf="!showLoader === true">

我已经创建了这样的会话和设置会话

showLoader = true;    
ngOnInit() {
        this.httpService.getOffer()
          .subscribe(
            data => this.offers = data
          )
        this.showLoader = false;
      }

调用setSession方法后,在下面的方法

中崩溃了
    @property (nonatomic, weak) IBOutlet AVCamPreviewView *previewView;

我也获得了相机许可

    // Check for device authorization
    [self checkDeviceAuthorizationStatus];


    // Create the AVCaptureSession
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    [self setSession:session];

   // Setup the preview view
   [[self previewView] setSession:session];
plist文件中的

。请帮我 ..... 这是我的代码CustomCameraViewController.m文件

    - (void)setSession:(AVCaptureSession *)session
{
   [(AVCaptureVideoPreviewLayer *)[self layer] setSession: session];
}

和AVCamPreviewView.m文件

    <key>NSCameraUsageDescription</key>
    <string>Allow us to scan documents and capture images.</string>

AVCamPreviewView.h文件

  #import "CustomCameraViewController.h"
  #import <AVFoundation/AVFoundation.h>
  #import <AssetsLibrary/AssetsLibrary.h>
  #import "PhotoEditViewController.h"
  #import "QuackPostData.h"
  #import "AVCamPreviewView.h"
  #import "Constants.h"
  #import <Photos/Photos.h>


  static void * CapturingStillImageContext =  &CapturingStillImageContext;
  static void * RecordingContext = &RecordingContext;
  static void * SessionRunningAndDeviceAuthorizedContext = &SessionRunningAndDeviceAuthorizedContext;

  @interface CustomCameraViewController ()    <UINavigationControllerDelegate,          UIImagePickerControllerDelegate,AVCaptureFileOutputRecordingDelegate,UIActionSheetDelegate,AVCaptureVideoDataOutputSampleBufferDelegate>
 {
    BOOL isFromEditView;
 }

     @property (nonatomic) UIImagePickerController *imagePickerController;

    // For use in the storyboards.
     @property (nonatomic, weak) IBOutlet AVCamPreviewView *previewView;
    @property (nonatomic, weak) IBOutlet UIButton *cameraButton;
    @property (nonatomic, weak) IBOutlet UIButton *stillButton;
    @property (nonatomic, weak) IBOutlet UIButton *flashButton;
    @property (nonatomic, weak) IBOutlet UIButton *photosButton;
    @property (nonatomic, weak) IBOutlet UIButton *cancelButton;

  - (IBAction)changeCamera:(id)sender;
  - (IBAction)snapStillImage:(id)sender;
  - (IBAction)cancelButtonTouched:(id)sender;
  - (IBAction)photoButtonTouched:(id)sender;
  - (IBAction)flashButtonTouched:(id)sender;


    // Session management.
     @property (nonatomic) dispatch_queue_t sessionQueue; // Communicate with the session and other session objects on this queue.
     @property (nonatomic) AVCaptureSession *session;
     @property (nonatomic) AVCaptureDeviceInput *videoDeviceInput;
     @property (nonatomic) AVCaptureMovieFileOutput *movieFileOutput;
     @property (nonatomic) AVCaptureStillImageOutput *stillImageOutput;

    // Utilities.
         @property (nonatomic) UIBackgroundTaskIdentifier backgroundRecordingID;
         @property (nonatomic, getter = isDeviceAuthorized) BOOL deviceAuthorized;
         @property (nonatomic, readonly, getter = isSessionRunningAndDeviceAuthorized) BOOL sessionRunningAndDeviceAuthorized;
         @property (nonatomic) BOOL lockInterfaceRotation;
         @property (nonatomic) id runtimeErrorHandlingObserver;

        @end


   @implementation CustomCameraViewController

     - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
   {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     if (self) {
      // Custom initialization
     }
       return self;
   }


   - (BOOL)isSessionRunningAndDeviceAuthorized
 {
      return [[self session] isRunning] && [self isDeviceAuthorized];
 }

  + (NSSet *)keyPathsForValuesAffectingSessionRunningAndDeviceAuthorized
 {
    return [NSSet setWithObjects:@"session.running", @"deviceAuthorized", nil];
 }

    - (void)viewDidLoad
 {
    [super viewDidLoad];


//adding border to photo button
   CALayer * layer = [_photosButton layer];
   [layer setMasksToBounds:YES];
   [layer setCornerRadius:0.0]; //when radius is 0, the border is a rectangle
    [layer setBorderWidth:0.5];
     [layer setBorderColor:[[UIColor blackColor] CGColor]];


// Assign Nil and fresh 
   [QuackPostData Instance].quack_NewImage = NO ;
   [QuackPostData Instance].quack_Image = nil;



// Create the AVCaptureSession
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    [self setSession:session];

// Setup the preview view
    [[self previewView] setSession:session];

// Check for device authorization
    [self checkDeviceAuthorizationStatus];

     [self setLatestPhoto:[self photosButton]];

    [self customFontPrepare];




     dispatch_queue_t sessionQueue = dispatch_queue_create("session queue", DISPATCH_QUEUE_SERIAL);
    [self setSessionQueue:sessionQueue];


    dispatch_async(sessionQueue, ^{
        [self setBackgroundRecordingID:UIBackgroundTaskInvalid];

        NSError *error = nil;

        AVCaptureDevice *videoDevice = [CustomCameraViewController deviceWithMediaType:AVMediaTypeVideo preferringPosition:AVCaptureDevicePositionBack];
            AVCaptureDeviceInput *videoDeviceInput =   [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];


    if (error)
    {
        NSLog(@"%@", error);
    }

    if ([session canAddInput:videoDeviceInput])
    {
        [session addInput:videoDeviceInput];
        [self setVideoDeviceInput:videoDeviceInput];

        dispatch_async(dispatch_get_main_queue(), ^{

            ((AVPlayerLayer *)[self previewView]).videoGravity = AVLayerVideoGravityResizeAspectFill;
            ((AVPlayerLayer *)[self previewView]).bounds = ((AVPlayerLayer *)[self previewView]).bounds;
        //
            [[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] setVideoOrientation:(AVCaptureVideoOrientation)[self interfaceOrientation]];
        });
    }

    AVCaptureDevice *audioDevice = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio] firstObject];
    AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error];

    if (error)
    {
        NSLog(@"%@", error);
    }

    if ([session canAddInput:audioDeviceInput])
    {
        [session addInput:audioDeviceInput];
    }

    AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
    if ([session canAddOutput:movieFileOutput])
    {
        [[self session] startRunning];
        [session addOutput:movieFileOutput];
        AVCaptureConnection *connection = [movieFileOutput connectionWithMediaType:AVMediaTypeVideo];
        if ([connection isVideoStabilizationSupported])
            [connection setEnablesVideoStabilizationWhenAvailable:YES];
        [self setMovieFileOutput:movieFileOutput];
    }

    AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    if ([session canAddOutput:stillImageOutput])
    {
        [stillImageOutput setOutputSettings:@{AVVideoCodecKey :    AVVideoCodecJPEG}];
        [session addOutput:stillImageOutput];
        [self setStillImageOutput:stillImageOutput];
    }
});
   }

0 个答案:

没有答案