首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在定制相机(Swift)期间,App正在悄然崩溃。

在定制相机(Swift)期间,App正在悄然崩溃。
EN

Stack Overflow用户
提问于 2016-03-04 02:05:32
回答 1查看 465关注 0票数 2

这个应用程序在这个函数中随机点崩溃。我认为我需要缩小规模,但我不确定。我对图像的唯一要求是,它保持一个正方形,并且保持体面的大小,因为我需要它足够大,以获得整个屏幕的宽度。

这里有一个错误,有时伴随着崩溃:

警告:无法加载任何目标-C类信息。这将大大降低现有类型信息的质量。

代码语言:javascript
复制
@IBAction func didPressTakePhoto(sender: UIButton) {
    self.previewLayer?.connection.enabled = false
    if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {
        videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
        stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {(sampleBuffer, error) in
            if (sampleBuffer != nil) {
                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                let dataProvider = CGDataProviderCreateWithCFData(imageData)
                let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, CGColorRenderingIntent.RenderingIntentDefault)
                var image = UIImage()

                if UIDevice.currentDevice().orientation == .Portrait{
                    image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)
                }else if UIDevice.currentDevice().orientation == .LandscapeLeft{
                    image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Up)
                }else if UIDevice.currentDevice().orientation == .LandscapeRight{
                    image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Down)
                }

                //Crop the image to a square
                let imageSize: CGSize = image.size
                let width: CGFloat = imageSize.width
                let height: CGFloat = imageSize.height
                if width != height {
                    let newDimension: CGFloat = min(width, height)
                    let widthOffset: CGFloat = (width - newDimension) / 2
                    let heightOffset: CGFloat = (height - newDimension) / 2
                    UIGraphicsBeginImageContextWithOptions(CGSizeMake(newDimension, newDimension), false, 0.0)
                    image.drawAtPoint(CGPointMake(-widthOffset, -heightOffset), blendMode: .Copy, alpha: 1.0)
                    image = UIGraphicsGetImageFromCurrentImageContext()
                    let imageData: NSData = UIImageJPEGRepresentation(image, 0.1)!
                    UIGraphicsEndImageContext()
                    self.captImage = UIImage(data: imageData)!
                }

            }
            self.performSegueWithIdentifier("fromCustomCamera", sender: self)
        })

    }


}

这段代码在我的viewDidAppear中运行,当我拍照时,stillImageOutput返回零。

代码语言:javascript
复制
if self.isRunning == false{
        captureSession = AVCaptureSession()
        captureSession!.sessionPreset = AVCaptureSessionPresetPhoto

        let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

        var error: NSError?

        do {
            input = try AVCaptureDeviceInput(device: backCamera)
        } catch let error1 as NSError {
            error = error1
            print(error)
            input = nil
        }

        if error == nil && captureSession!.canAddInput(input) {
            captureSession!.addInput(input)
            stillImageOutput = AVCaptureStillImageOutput()
            stillImageOutput!.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
            if captureSession!.canAddOutput(stillImageOutput) {
                captureSession!.addOutput(stillImageOutput)
                previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                previewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
                previewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.Portrait
                previewView.layer.addSublayer(previewLayer!)

                captureSession!.startRunning()
                self.isRunning = true



            }
        }

    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-05 05:53:31

修好了。它崩溃的原因实际上是因为我的图像太大了。我得把它们压缩。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35786724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档