ALCameraViewController 3.1

ALCameraViewController 3.1

测试已测试
Lang语言 SwiftSwift
许可协议 MIT
Released上次发布2019年10月
SPM支持 SPM

Alex LittleohnGuillaume Bellut维护。



  • Alex Littlejohn 和 Guillaume Bellut

具有自定义图片选择器和图片裁剪功能的相机视图控制器。

camera
cropper
library
permissions

功能

  • 前置和后置相机
  • 简洁的外观
  • 带有权限检查的自定义图片选择器
  • 图片裁剪
  • 闪光灯
  • 缩放
  • 点击对焦

安装 & 要求

此项目需要 Xcode 9 运行和 Swift 4 编译

注意:此库使用 AVFoundation 相机 API 的 AVFoundation,这在 iOS 模拟器中不可用。您需要真实设备才能运行它。

CocoaPods
将以下内容添加到您的 Podfile 中

pod 'ALCameraViewController'

Carthage

github "alexlittlejohn/ALCameraViewController"

隐私(iOS 10)

如果您正在使用 iOS 10 或更高版本构建应用程序,则需要在您的应用程序中添加两个隐私密钥以允许使用相机和照片库,否则您的应用程序将崩溃。

将以下密钥添加到您的 Info.plist 中,您需要添加字符串以提供用户提示时所需的描述。

    NSPhotoLibraryUsageDescription
    NSCameraUsageDescription

使用方法

使用此组件非常简单。
import ALCameraViewController 添加到控制器文件顶部。

在 viewController 中

let cameraViewController = CameraViewController { [weak self] image, asset in
	// Do something with your image here.
	self?.dismiss(animated: true, completion: nil)
}

present(cameraViewController, animated: true, completion: nil)

参数

针对 CameraViewController 有许多可选的配置选项。

init(croppingParameters: CroppingParameters = CroppingParameters(),
     allowsLibraryAccess: Bool = true,
     allowsSwapCameraOrientation: Bool = true,
     allowVolumeButtonCapture: Bool = true,
     completion: @escaping CameraViewCompletion)

裁剪参数结构接受以下参数

init(isEnabled: Bool = false,
     allowResizing: Bool = true,
     allowMoving: Bool = true,
     minimumSize: CGSize = CGSize(width: 60, height: 60))

成功参数返回一个 UIImage? 和一个 PHAsset?,用于更高级的使用场景。
如果用户取消了照片捕获,这两个选项都将为 nil

typealias CameraViewCompletion = (UIImage?, PHAsset?) -> Void

注意:为防止保留循环,最好在成功参数中使用 [weak self] 引用

其他使用选项

您还可以单独实例化图像选择器组件。

let croppingEnabled = true

/// Provides an image picker wrapped inside a UINavigationController instance
let imagePickerViewController = CameraViewController.imagePickerViewController(croppingEnabled: croppingEnabled) { [weak self] image, asset in
		// Do something with your image here.
	 	// If cropping is enabled this image will be the cropped version

    self?.dismiss(animated: true, completion: nil)
}

present(imagePickerViewController, animated: true, completion: nil)

为了获得更多的控制,您可以直接创建它。

注意:这种方法需要熟悉苹果提供的 PhotoKit 库

import Photos

let imagePickerViewController = PhotoLibraryViewController()
imagePickerViewController.onSelectionComplete = { asset in

		// The asset could be nil if the user doesn't select anything
		guard let asset = asset else {
			return
		}

    // Provides a PHAsset object
		// Retrieve a UIImage from a PHAsset using
		let options = PHImageRequestOptions()
    options.deliveryMode = .highQualityFormat
    options.isNetworkAccessAllowed = true

		PHImageManager.default().requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode: .aspectFill, options: options) { image, _ in
        if let image = image {
						// Do something with your image here
        }
    }
}

present(imagePickerViewController, animated: true, completion: nil)

许可协议

ALCameraViewController 在 MIT 许可下可用。更多信息请参阅 LICENSE 文件。