Thread 1: signal SIGABRT 是什么意思?

What does Thread 1: signal SIGABRT mean?

我正在开发一款使用前置摄像头拍照但显示后置摄像头的应用。我的代码中没有警告或错误。当我 运行 时一切正常。摄像头视图加载,您可以看到后置摄像头看到的内容。一切正常,直到我按下照片按钮。然后不能再按下按钮,AppDelegate.swift 打开并突出显示行 class AppDelegate: UIResponder, UIApplicationDelegate { 并给出错误:"Thread 1: signal SIGABRT"

这是我的 ViewController.swift:

import UIKit
import AVFoundation


class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var captureSession : AVCaptureSession?
var stillImageOutput : AVCaptureStillImageOutput?
var stillImageOutput2 : AVCaptureStillImageOutput?
var previewLayer : AVCaptureVideoPreviewLayer?
var captureSession2 : AVCaptureSession?



@IBOutlet weak var cameraView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}






override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    previewLayer?.frame = cameraView.bounds
}






override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    captureSession = AVCaptureSession()
    captureSession?.sessionPreset = AVCaptureSessionPreset1920x1080




    let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)



    do {
    let input = try AVCaptureDeviceInput(device: backCamera)
        captureSession?.addInput(input)
    }catch{

    }






    stillImageOutput = AVCaptureStillImageOutput()
    stillImageOutput?.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]

    if ((captureSession?.canAddOutput(stillImageOutput)) != nil) {
        captureSession?.addOutput(stillImageOutput)



        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
        previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.Portrait
        cameraView.layer.addSublayer(previewLayer!)
        captureSession?.startRunning()


    }


}

func ohterCamera() {
    captureSession2 = AVCaptureSession()
    captureSession2?.sessionPreset = AVCaptureSessionPreset1920x1080

    let frontCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)


    do {
        let input = try AVCaptureDeviceInput(device: frontCamera)
        captureSession?.addInput(input)
    }catch{

    }


    stillImageOutput2 = AVCaptureStillImageOutput()
    stillImageOutput2?.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]

    if ((captureSession2?.canAddOutput(stillImageOutput2)) != nil) {
        captureSession2?.addOutput(stillImageOutput2)

        captureSession2?.startRunning()



    }


}
var savedImage:UIImage!
func didTakePhoto() {
    if let videoConection = stillImageOutput2?.connectionWithMediaType(AVMediaTypeVideo){
        videoConection.videoOrientation = AVCaptureVideoOrientation.Portrait
        stillImageOutput2?.captureStillImageAsynchronouslyFromConnection(videoConection, completionHandler: { (sampleBuffer, ErrorType) -> Void in
            if sampleBuffer != nil {
                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                let dataProvider = CGDataProviderCreateWithCFData(imageData)
                let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, .RenderingIntentDefault)

                self.savedImage = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)


            }
        })
    }
}

@IBAction func takePhotoBtn(sender: UIButton) {
    didTakePhoto()
    UIImageWriteToSavedPhotosAlbum(savedImage, nil, nil, nil)
}

} 

这里是 AppDelegate.Swift:

mport UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    return true
}

func applicationWillResignActive(application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}

有谁知道为什么我会收到此错误,and/or我该如何解决? 从我用谷歌搜索时发现的情况来看,这意味着连接断开,但据我所知,所有连接都很好...

该消息仅表示应用程序崩溃,在大多数情况下,这是一个 found nil while unwrapping optional 错误。

错误发生在这一行:

UIImageWriteToSavedPhotosAlbum(savedImage, nil, nil, nil)

顾名思义,captureStillImageAsynchronouslyFromConnection 方法是异步工作的。调用UIImageWriteToSavedPhotosAlbum时照片还没有创建。

解决方法:将行移到self.savedImage = UIImage(...

之后的完成块中