FMCamera
一个简单的相机视图,可以拍照和捕获照片。相机视图可根据给定的大小裁剪照片和视频。您可以捕获方形视频。有两种协议提供视图控制器与 fmcamera 视图之间的通信。如果需要,您可以设置最大图片大小并配置音频、视频和图片设置。此外,您还可以为视频获取缩略图。
使用 XCode 11.3.1 (Swift 5) 构建
截图
示例
要运行示例项目,请克隆仓库,然后首先从 Example 目录中运行 pod install
。
安装
手动
- 克隆此仓库
- 导航到项目文件夹
- 将
源
复制到您的项目中
使用 Cocoapods
FMCamera 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod 'FMCamera'
权限
将以下键添加到您的 info.plist
文件中。不要忘记更改描述文本。
<key>NSPhotoLibraryUsageDescription</key>
<string>Photo library usage description</string>
<key>NSMicrophoneUsageDescription</key>
<string>Microphone usage description</string>
<key>NSCameraUsageDescription</key>
<string>Camera usage description</string>
使用
- 在您希望使用 FMCamera 的类中导入
import FMCamera
。 - 您可以在 Storyboard 中创建相机视图或使用代码创建。使用名为
FMCamera
的类。 - 如有必要,更新配置参数。
- 使用
configure()
函数配置 fmcamera。 - 使用图片、视频或两者的代理。
- 为图片和/或视频协议编写扩展。
- 协议函数将返回图像或视频的 URL,使用它们。
代码
在创建 FMCamera 对象后(例如,命名为 fmCamera
)的视图控制器中。
配置
override func viewDidLoad() {
super.viewDidLoad()
fmCapturePhotoDelegate = self
fmCaptureVideoDelegate = self
// You can update configuration parameters here
// or you can just use default ones.
fmCamera.configure()
}
捕获
// Take Photo
fmCamera.takePhoto()
// Capture Video
if !fmCamera.isCameraRecording {
fmCamera.startRecording()
} else {
fmCamera.stopRecording()
}
协议
extension ViewController: FMCaptureVideoProtocol {
func recordingStarted(_ error: Error?) {
if let error = error {
print(error.localizedDescription)
}
}
func recordingFinished(_ videoUrl: URL?, _ error: Error?) {
if let error = error {
print(error.localizedDescription)
} else {
/// Use recorded video url
}
}
}
extension ViewController: FMCapturePhotoProtocol {
func captured(_ image: UIImage, data: Data) {
/// Use captured and optimized image
}
}
配置参数
代码文档中增加了使用示例。按选项+单击参数可查看使用示例。我们有两种类型的配置参数。在调用fmcamera.configure()
之前,以及在拍照之前。
在调用 configure() 之前
/// Set session preset with<br/>
var sPreset: AVCaptureSession.Preset = .medium
/// Set this parameter to TRUE if you want to use
/// FMCamera just for photo capturing.<br/>
var setForPhotoCapturingOnly: Bool = false
///Flash mode for capturing.<br/>
var flashMode: AVCaptureDevice.FlashMode = .auto
/// Capture photo format.<br/>
var photoSettingsFormat: [String: Any]?
/// Video settings for recording video.<br/>
var videoSettings: [String: Any] = [:]
/// Audio settings for audio capturing<br/>
var audioSettings: [String: Any] = [:]
拍照前
/// Update this for save the photo to your Photos.
var willSavePhotoToPhotos: Bool = false
/// Update this for save the video to your Photos.
var willSaveVideoToPhotos: Bool = false
/// Save original or save reduced image to photo roll.
///Works if `willSaveVideoToPhotos` is true.
var saveReducedImageToPhotos: Bool = false
/// Set max picture file size in bytes.
var maxPictureFileSize: Int = 400000
/// Set this to false if you want to optimize images yourself.
var optimizeImage: Bool = true
/// Decide if the captured photo rotates upwards.
///After capturing, photo always oriented upwards if you set this flag to true.
var rotateCapturedPhotoUpwards: Bool = false
https://stackoverflow.com/a/44917862
https://www.appcoda.com/avfoundation-swift-guide/
https://stackoverflow.com/a/32041649
作者
gokhanmandaci, [email protected]
许可证
FMCamera遵从MIT许可证。更详细的信息请查看LICENSE文件。