Swift相簿選擇和拍照功能
專案開發中經常會有更改使用者頭像和上傳圖片等場景會用到圖片的選擇。為了方便以後的使用,整合了一個簡單的選擇圖片的功能
這次是用swift 3.0 寫的,主要是建立了一個控制器YMImgPickerVC.swift
檔案程式碼如下
//圖片選擇器
import UIKit
var width = UIScreen.main.bounds.width
var heigth = UIScreen.main.bounds.height
class YMImgPickerVC: UIViewController {
var bgView : UIView?
var cancelBtn : UIButton?
var photoBtn : UIButton?
var cameraBtn : UIButton?
var callbackWithImg : ((_ imgData:Data) -> Void)? = nil //閉包返回圖片資料
var imagePickerCtr : UIImagePickerController!
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
initImagePickerCtr()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
//設定介面
extension YMImgPickerVC {
fileprivate func setupUI(){
//背景View
bgView = UIView(frame: CGRect(x: 0, y: UIScreen.main.bounds.height - 127, width: UIScreen.main.bounds.width, height: 127))
bgView?.backgroundColor = UIColor.gray
guard let bgview1 = bgView else {
return ;
}
view.addSubview(bgview1)
//取消按鈕
cancelBtn = UIButton(frame: CGRect(x: 0, y: 87, width: UIScreen.main.bounds.width, height: 40))
cancelBtn?.setTitle("取消", for: .normal)
cancelBtn?.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
cancelBtn?.setTitleColor(UIColor.black, for: .normal)
cancelBtn?.tag = 10001
cancelBtn?.backgroundColor = UIColor.white
guard let btn = cancelBtn else {
return ;
}
bgView?.addSubview(btn)
//相簿選擇按鈕
photoBtn = UIButton(frame: CGRect(x: 0, y: 41, width: UIScreen.main.bounds.width, height: 40))
photoBtn?.setTitle("從相簿選擇", for: .normal)
photoBtn?.setTitleColor(UIColor.black, for: .normal)
photoBtn?.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
photoBtn?.tag = 10002
photoBtn?.backgroundColor = UIColor.white
guard let btn1 = photoBtn else {
return ;
}
bgView?.addSubview(btn1)
//照相按鈕
cameraBtn = UIButton(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 40))
cameraBtn?.setTitle("拍照", for: .normal)
cameraBtn?.setTitleColor(UIColor.black, for: .normal)
cameraBtn?.tag = 10003
cameraBtn?.backgroundColor = UIColor.white
cameraBtn?.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
guard let btn2 = cameraBtn else {
return ;
}
bgView?.addSubview(btn2)
}
fileprivate func initImagePickerCtr(){
imagePickerCtr = UIImagePickerController()
imagePickerCtr.delegate = self
//設定是否可以管理已經存在的圖片或視訊
imagePickerCtr.allowsEditing = true
}
}
//按鈕帶點選的方法
extension YMImgPickerVC {
@objc fileprivate func btnClick(btn : UIButton){
switch btn.tag {
case 10001:
YMlog(message:btn.currentTitle!+"\(btn.tag)")
self.dismiss(animated: true, completion: {
})
case 10002:
//相簿
YMlog(message:btn.currentTitle!+"\(btn.tag)")
chooseImg(type: .photoLibrary)
case 10003:
//相機
YMlog(message:btn.currentTitle!+"\(btn.tag)")
chooseImg(type: .camera)
default:
break
}
}
//相簿選擇或拍照
func chooseImg(type:UIImagePickerControllerSourceType){
imagePickerCtr.sourceType = type
if UIImagePickerController.isSourceTypeAvailable(type){
self.present(imagePickerCtr, animated: true, completion: {
})
}else{
YMPrint(message: "沒有許可權")
}
}
}
//MARK: - UIImagePickerControllerDelegate
extension YMImgPickerVC : UIImagePickerControllerDelegate, UINavigationControllerDelegate{
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let img = info[UIImagePickerControllerOriginalImage] as? UIImage
//壓縮圖片
//預設壓縮到100KB
let imgData = compressImage(image: img ?? UIImage(), maxLength: 1024*100) // UIImageJPEGRepresentation(img!, 0.5)
YMPrint(message: imgData)
picker.dismiss(animated: true, completion: {
guard let data = imgData else {
return ;
}
self.callbackWithImg!(data)
})
self.dismiss(animated: true) {
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true) {
}
self.dismiss(animated: true) {
}
}
}
//指定壓縮圖片大小
extension YMImgPickerVC {
/// 預縮圖片到指定位元組
///
/// - Parameters:
/// - image: 需要壓縮的圖片
/// - maxLength: 壓縮的大小
/// - Returns: 返回壓縮的圖片
fileprivate func compressImage(image : UIImage, maxLength: Int ) -> Data? {
var compress:CGFloat = 0.9
var data = UIImageJPEGRepresentation(image, compress)
while (data?.count)! > maxLength && compress > 0.01 {
compress -= 0.02
data = UIImageJPEGRepresentation(image, compress)
}
return data
}
}
具體用法:
let imgPickerVC = YMImgPickerVC()
imgPickerVC.modalPresentationStyle = .overCurrentContext //模態檢視的展現形式
imgPickerVC.view.backgroundColor = UIColor(white: 0.3, alpha: 0.7) //檢視的背景顏色
imgPickerVC.callbackWithImg = {(imgData:Data) in
let cell = self.tableView(tableView, cellForRowAt: indexPath)
cell.imageView?.image = UIImage(data: imgData)
YMPrint(message: imgData)
self.headerView.image = UIImage(data: imgData)
self.tableView?.tableHeaderView = self.headerView
}
git地址:https://github.com/juliankeji/YMTool/tree/master/YMTool
相關文章
- 直播平臺軟體開發,Android 10 拍照和相簿選擇Android
- Android上傳圖片之呼叫系統拍照和從相簿選擇圖片Android
- android實現拍照、相簿選圖、裁剪功能,相容7.0以及小米Android
- Android 圓形頭像 相簿和拍照裁剪選取Android
- Android拍照,相簿選擇圖片以及Android6.0許可權管理Android
- 處理input file限制只能拍照不能選相簿
- 相簿圖片多選、反選 swift實現Swift
- iOS 仿微信相簿選擇照片imagePicker(Swift) 序號 預覽縮圖iOSSwift
- 專案需求討論 - WebView下拍照及圖片選擇功能WebView
- Android 拍照及相簿選取圖片功能,已適配Android6.0、7.0、8.0Android
- Android中呼叫攝像頭拍照儲存,並在相簿中選擇圖片顯示Android
- Android 呼叫系統相機拍照 . 選取本地相簿Android
- 仿微信iOS相簿選擇 MTImagePickeriOS
- Android呼叫系統相簿和相機拍照Android
- Android 拍照、選擇圖片並裁剪Android
- 直播平臺搭建,Android手機拍照和手機相簿選取圖片的工具Android
- 鴻蒙無許可權實現圖片選擇拍照和錄影片鴻蒙
- 【Swift】彈出日曆選擇框Swift
- Android本地圖片上傳(拍照+相簿)Android地圖
- 短視訊程式開發,Android:呼叫系統拍照和相簿Android
- jquery樣式選擇功能jQuery
- iOS 開發選擇OC還是Swift?iOSSwift
- 使用XGBoost在Python中的功能重要性和功能選擇Python
- MUI – IOS系統,相簿選擇照片後,點選確定按鈕無反應UIiOS
- 微信小程式-拍照或選擇圖片並上傳檔案微信小程式
- iOS 呼叫系統相簿拍照時顯示英文問題iOS
- JQuery選擇器——可見性篩選選擇器和屬性篩選選擇器jQuery
- vue iOS 呼叫系統相簿拍照時顯示英文問題VueiOS
- 【Swift Objective C】強勁的日期選擇控制元件SwiftObject控制元件
- js選擇物件和jq選擇物件的區別JS物件
- Android 頭像選擇功能實戰Android
- 使用Webcam實現拍照功能Web
- Android WebView 實現檔案選擇、拍照、錄製視訊、錄音AndroidWebView
- 選擇排序和快速排序排序
- 從Go、Swift出發:語言的選擇需謹慎GoSwift
- Java選擇框和單選按鈕Java
- jquery匹配具有指定字元的選擇器功能jQuery字元
- 商品屬性的選擇功能的實現