我可以做除了保存到图书馆以外的所有人,有人可以帮忙吗?
这是我打开相机的代码:
Code Support for Swift 3.1 & 4.0 version:
首先,我们必须在Project的.pList文件中进行权限设置: –
1)相机
<key>NSCameraUsageDescription</key><string>This app will use camera.</string>
2)图片库
<key>NSPhotolibraryUsageDescription</key><string>You can select photos to attach to reports.</string>
3)保存到照片库
<key>NSPhotolibraryAddUsageDescription</key><string>Please allow access to save photo in your photo library</string>
我们需要打开.pilst文件作为源代码类型,然后在里面添加权限 –
之后
import UIKitclass VIEwController: UIVIEwController,UINavigationControllerDelegate,UIImagePickerControllerDelegate { @IBOutlet weak var imageTake: UIImageVIEw! var imagePicker: UIImagePickerController! overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() } //MARK: - Take image @IBAction func takePhoto(_ sender: UIbutton) { imagePicker = UIImagePickerController() imagePicker.delegate = self imagePicker.sourceType = .camera present(imagePicker,animated: true,completion: nil) } //MARK: - Saving Image here @IBAction func save(_ sender: AnyObject) { UIImageWritetoSavedPhotosAlbum(imageTake.image!,self,#selector(image(_:dIDFinishSavingWithError:contextInfo:)),nil) } //MARK: - Add image to library func image(_ image: UIImage,dIDFinishSavingWithError error: Error?,contextInfo: UnsafeRawPointer) { if let error = error { // we got back an error! let ac = UIAlertController(Title: "Save error",message: error.localizedDescription,preferredStyle: .alert) ac.addAction(UIAlertAction(Title: "OK",style: .default)) present(ac,animated: true) } else { let ac = UIAlertController(Title: "Saved!",message: "Your altered image has been saved to your photos.",animated: true) } } //MARK: - Done image capture here func imagePickerController(_ picker: UIImagePickerController,dIDFinishPickingMediawithInfo info: [String : Any]) { imagePicker.dismiss(animated: true,completion: nil) imageTake.image = info[UIImagePickerControllerOriginalimage] as? UIImage }}
Swift 4.2 Code update –
class VIEwController: UIVIEwController,UINavigationControllerDelegate { @IBOutlet weak var imageTake: UIImageVIEw! var imagePicker: UIImagePickerController! enum ImageSource { case photolibrary case camera } overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() } //MARK: - Take image @IBAction func takePhoto(_ sender: UIbutton) { guard UIImagePickerController.isSourceTypeAvailable(.camera) else { selectimageFrom(.photolibrary) return } selectimageFrom(.camera) } func selectimageFrom(_ source: ImageSource){ imagePicker = UIImagePickerController() imagePicker.delegate = self switch source { case .camera: imagePicker.sourceType = .camera case .photolibrary: imagePicker.sourceType = .photolibrary } present(imagePicker,completion: nil) } //MARK: - Saving Image here @IBAction func save(_ sender: AnyObject) { guard let selectedImage = imageTake.image else { print("Image not found!") return } UIImageWritetoSavedPhotosAlbum(selectedImage,nil) } //MARK: - Add image to library @objc func image(_ image: UIImage,contextInfo: UnsafeRawPointer) { if let error = error { // we got back an error! showAlertWith(Title: "Save error",message: error.localizedDescription) } else { showAlertWith(Title: "Saved!",message: "Your image has been saved to your photos.") } } func showAlertWith(Title: String,message: String){ let ac = UIAlertController(Title: Title,message: message,preferredStyle: .alert) ac.addAction(UIAlertAction(Title: "OK",style: .default)) present(ac,animated: true) } } extension VIEwController: UIImagePickerControllerDelegate{ func imagePickerController(_ picker: UIImagePickerController,dIDFinishPickingMediawithInfo info: [UIImagePickerController.InfoKey : Any]){ imagePicker.dismiss(animated: true,completion: nil) guard let selectedImage = info[.originalimage] as? UIImage else { print("Image not found!") return } imageTake.image = selectedImage }}总结
以上是内存溢出为你收集整理的swift3 – swift拍照并保存到照片库全部内容,希望文章能够帮你解决swift3 – swift拍照并保存到照片库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)