我正在开发一款使用前置摄像头拍照的应用程序,但显示后置摄像头。我的代码中没有警告或错误。当我运行它一切正常。摄像机视图加载,您可以看到后置摄像头看到的内容。一切正常,直到我按下照片按钮。然后按钮不能再被按下,AppDelegate.swift打开并突出显示行class AppDelegate: UIResponder, UIApplicationDelegate {
并给出错误:"线程1:信号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:.
}
}
有谁知道我为什么会收到此错误,和/或我如何修复它? 从我用Google搜索时发现,这意味着连接断开,但据我所知,所有连接都很好......
答案 0 :(得分:0)
该消息仅表示应用程序崩溃,在大多数情况下,它是found nil while unwrapping optional
错误。
发生错误的是这一行:
UIImageWriteToSavedPhotosAlbum(savedImage, nil, nil, nil)
顾名思义,方法captureStillImageAsynchronouslyFromConnection
异步工作。调用UIImageWriteToSavedPhotosAlbum
后,尚未创建照片。
解决方案:在self.savedImage = UIImage(...