PGMCameraKit 0.1.8

PGMCameraKit 0.1.8

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2016年4月
SPM支持SPM

Pablo GM维护。



  • Pablo GM

CameraKit

Swift库,用于提供创建相机视图所需的全部配置

  • 开始/暂停/恢复/停止录制
  • 视频压缩
  • 从媒体库保存/获取视频和图像
  • 设置最大视频持续时间阈值
  • 跟随相机方向变化
  • 前后摄像头
  • 闪光灯模式
  • 视频/静态图像模式
  • 输出质量

用法

要运行示例项目,请克隆仓库,然后首先从示例目录运行pod install

要求

安装

CameraKit可以通过CocoaPods获取。要安装它,只需将以下行添加到Podfile中

pod "PGMCameraKit"

如何使用

初始化Camera Kit

let cameraManager       = PGMCameraKit()

初始化Camera Kit Helper(用于保存图像/视频、从媒体库检索图像/视频以及压缩视频输出的实用函数)

let helper              = PGMCameraKitHelper()

请求用户授权相机权限

cameraManager.askUserForCameraPermissions({ [unowned self] permissionGranted in

    if permissionGranted {
        self.addCameraToView()
    }
    else {
        self.addCameraAccessDeniedPopup("Go to settings and grant acces to the camera device to use it.")
    }
})

设置视频时间限制。

cameraManager.maxRecordedDuration = 4.0 // secs

监听器

// Errors
cameraManager.addCameraErrorListener( { error in


})

// Time progress
cameraManager.addCameraTimeListener( { time in

    print("Time elapsed: \(time) seg")
})

// Video time limit
cameraManager.addMaxAllowedLengthListener({ [unowned self] (videoURL, error, localIdentifier) -> () in

    if let err = error {
        print("Error \(err)")
    }
    else {

        if let url = videoURL {

            print("Saved video from local url \(url) with uuid \(localIdentifier)")

            let data = NSData(contentsOfURL: url)!

            print("Byte Size Before Compression: \(data.length / 1024) KB")

        }
    }
})

开始录制视频

cameraManager.startRecordingVideo( {(error)->() in

    if let err = error {
        print("Error ocurred: \(err)")
    }
})

暂停录制

cameraManager.pauseRecordingVideo()

恢复录制

cameraManager.resumeRecordingVideo()

停止录制

cameraManager.stopRecordingVideo( { (videoURL, error, localIdentifier) -> () in

    if let err = error {
        print("Error ocurred: \(err)")
    }
    else {
        print("Video url: \(videoURL) with unique id \(localIdentifier)")
    }
})

视频压缩

// The compress file extension will depend on the output file type
self.helper.compressVideo(url, outputURL: self.cameraManager.tempCompressFilePath("mp4"), outputFileType: AVFileTypeMPEG4, handler: { session in

    if let currSession = session {

        print("Progress: \(currSession.progress)")

        print("Save to \(currSession.outputURL)")

        if currSession.status == .Completed {

            if let data = NSData(contentsOfURL: currSession.outputURL!) {

            print("File size after compression: \(data.length / 1024) KB")

            // Play compressed video
            dispatch_async(dispatch_get_main_queue(), {

                let player  = AVPlayer(URL: currSession.outputURL!)
                let layer   = AVPlayerLayer(player: player)
                layer.frame = self.view.bounds
                self.view.layer.addSublayer(layer)
                player.play()

                print("Playing video...")
            })
            }
        }
        else if currSession.status == .Failed
        {
            print(" There was a problem compressing the video maybe you can try again later. Error: \(currSession.error!.localizedDescription)")
        }
    }
})

支持

支持iOS8及以上。要构建用Swift 2.0编写的最新代码,需要XCode 7.0。

作者

pablogm, [email protected]

许可证

CameraKit可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。