将CGImage创建的UIImage保存到已保存的照片相册会导致我的应用程序崩溃

时间:2015-12-02 14:36:01

标签: ios swift uiimage cgimage avcapturedevice

我正试图在我的一个小练习应用程序中实现未压缩的照片,但我不能让它为我的生活工作。

这就是我捕捉照片的方式:

print("Shutter!")
if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) {
    stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
        (imageDataSampleBuffer, error) -> Void in

        if let cameraFrame = CMSampleBufferGetImageBuffer(imageDataSampleBuffer) {
            CVPixelBufferLockBaseAddress(cameraFrame, 0)
            let rawImageBytes = CVPixelBufferGetBaseAddress(cameraFrame)
            let bytesPerRow = CVPixelBufferGetBytesPerRow(cameraFrame)
            let width = CVPixelBufferGetWidth(cameraFrame)
            let height = CVPixelBufferGetHeight(cameraFrame)
            CVPixelBufferUnlockBaseAddress(cameraFrame, 0)

            let provider = CGDataProviderCreateWithData(nil, rawImageBytes, width * height * 4, nil)
            let bitsPerComponent = 8
            let bitsPerPixel = 32
            let colorSpaceRef = CGColorSpaceCreateDeviceRGB()
            let bitmapInfo: CGBitmapInfo = .ByteOrderDefault
            let renderingIntent: CGColorRenderingIntent = .RenderingIntentDefault

            if let imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow * 4, colorSpaceRef, bitmapInfo, provider, nil, false, renderingIntent) {
                print("Attempting to save image")
                let tiffImage = UIImage(CGImage: imageRef)
                UIImageWriteToSavedPhotosAlbum(tiffImage, nil, "image:didFinishSavingWithError:contextInfo:", nil)
            } else {
                print("CGImageCreate failed")
            }
        } else {
            print("CMSampleBufferGetImageBuffer failed")
        }
    }
}

执行那段代码打印“快门!”和“试图保存图像”到Xcode的输出面板,然后崩溃应用程序,导致Xcode向我展示了一堆我认为是汇编代码。毋庸置疑,这对我没有任何帮助。

我尝试在UIImageView中显示UIImage,这也导致应用程序崩溃。这是怎么回事?

0 个答案:

没有答案