我正在制作一个照相应用程序,用于拍照并保存到照片库中。我希望用户能够像共享实际相机应用程序一样,出于共享/编辑/删除照片的目的而进入照片库。为了澄清起见,我不想访问图像选择器来选择图像,然后将其重新加载到我的应用中。我想从我的应用程序中打开实际的照片应用程序,以便用户可以使用现有的内置照片应用程序功能在其中进行操作。这可能吗?它似乎可以在默认的Apple相机应用程序中运行,但在其他任何地方都没有看到它。我似乎找不到任何有关此的信息,因为当我在Google上搜索时,我一直在获取有关图像选择器的信息。
现在我拥有的最好的是,但是你离开了我的应用程序
UIApplication.shared.open(URL(string:"photos-redirect://")!)
答案 0 :(得分:0)
您可以实现
UIActivityViewController
系统提供了几种标准服务,例如将项目复制到粘贴板,将内容发布到社交媒体站点,通过电子邮件或SMS发送项目等等。应用程序还可以定义自定义服务。
https://developer.apple.com/documentation/uikit/uiactivityviewcontroller
答案 1 :(得分:-1)
创建一个新的swift文件,例如Camera.swift并将此代码复制到其中。
注意:您必须将隐私权限添加到info.plist。
import Foundation
import MobileCoreServices
import UIKit
import CoreLocation
class Camera {
var delegate: UIImagePickerControllerDelegate & UINavigationControllerDelegate
init(source: UIImagePickerControllerDelegate & UINavigationControllerDelegate) {
delegate = source
}
func PresentPhotoInput(target: UIViewController, canEdit: Bool = false) {
let picker = UIAlertController(title: "Select Photo Source", message: "Would you like to take a new photo or add one from your photo library?", preferredStyle: UIAlertControllerStyle.actionSheet)
picker.addAction(UIAlertAction(title: "Camera", style: UIAlertActionStyle.default, handler: { (action) in
self.PresentMultyCamera(target: target, canEdit: canEdit)
}))
picker.addAction(UIAlertAction(title: "Photo Library", style: UIAlertActionStyle.default, handler: { (action) in
self.PresentPhotoLibrary(target: target, canEdit: canEdit)
}))
picker.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.destructive, handler: { (action) in
// foo
}))
target.present(picker, animated: true)
}
func PresentPhotoLibrary(target: UIViewController, canEdit: Bool) {
if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) && !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum) {
return
}
let type = kUTTypeImage as String
let imagePicker = UIImagePickerController()
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
imagePicker.sourceType = .photoLibrary
if let availableTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary) {
if (availableTypes as NSArray).contains(type) {
/* Set up defaults */
imagePicker.mediaTypes = [type]
imagePicker.allowsEditing = canEdit
}
}
} else if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum) {
imagePicker.sourceType = .savedPhotosAlbum
if let availableTypes = UIImagePickerController.availableMediaTypes(for: .savedPhotosAlbum) {
if (availableTypes as NSArray).contains(type) {
imagePicker.mediaTypes = [type]
}
}
} else {
return
}
imagePicker.allowsEditing = canEdit
imagePicker.delegate = delegate
target.present(imagePicker, animated: true, completion: nil) // presents the imagepicker to the user
return
}
func PresentMultyCamera(target: UIViewController, canEdit: Bool) {
if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
return
}
let type1 = kUTTypeImage as String
//let type2 = kUTTypeMovie as String
let imagePicker = UIImagePickerController()
if UIImagePickerController.isSourceTypeAvailable(.camera) {
if let availableTypes = UIImagePickerController.availableMediaTypes(for: .camera) {
if (availableTypes as NSArray).contains(type1) {
imagePicker.mediaTypes = [type1/*, type2*/]
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.showsCameraControls = true
}
}
if UIImagePickerController.isCameraDeviceAvailable(.rear) {
imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.rear
} else if UIImagePickerController.isCameraDeviceAvailable(.front) {
imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.front
}
} else {
// show alert, no camera available
return
}
imagePicker.allowsEditing = canEdit
imagePicker.showsCameraControls = true
imagePicker.delegate = delegate
target.present(imagePicker, animated: true, completion: nil) // presents the imagepicker to the user
}
func PresentPhotoCamera(target: UIViewController, canEdit: Bool) {
if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
return
}
let type1 = kUTTypeImage as String
let imagePicker = UIImagePickerController()
if UIImagePickerController.isSourceTypeAvailable(.camera) {
if let availableTypes = UIImagePickerController.availableMediaTypes(for: .camera) {
if (availableTypes as NSArray).contains(type1) {
imagePicker.mediaTypes = [type1]
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
}
}
if UIImagePickerController.isCameraDeviceAvailable(.rear) {
imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.rear
} else if UIImagePickerController.isCameraDeviceAvailable(.front) {
imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.front
}
} else {
// show alert, no camera available
return
}
imagePicker.allowsEditing = canEdit
imagePicker.showsCameraControls = true
imagePicker.delegate = delegate
App.shared.isAuthenticating = true
target.present(imagePicker, animated: true, completion: nil) // presents the imagepicker to the user
}
// Video Camera
func PresentVideoCamera(target: UIViewController, canEdit: Bool) {
if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
return
}
let type1 = kUTTypeMovie as String
let imagePicker = UIImagePickerController()
if UIImagePickerController.isSourceTypeAvailable(.camera) {
if let availableTypes = UIImagePickerController.availableMediaTypes(for: .camera) {
if (availableTypes as NSArray).contains(type1) {
imagePicker.mediaTypes = [type1]
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.videoMaximumDuration = 15.0
}
}
if UIImagePickerController.isCameraDeviceAvailable(.rear) {
imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.rear
} else if UIImagePickerController.isCameraDeviceAvailable(.front) {
imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.front
}
} else {
// show alert, no camera available
return
}
imagePicker.allowsEditing = canEdit
imagePicker.showsCameraControls = true
imagePicker.delegate = delegate
target.present(imagePicker, animated: true, completion: nil) // presents the imagepicker to the user
}
// video library
func PresentVideoLibrary(target: UIViewController, canEdit: Bool) {
if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) && !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum) {
return
}
let type = kUTTypeMovie as String
let imagePicker = UIImagePickerController()
imagePicker.videoMaximumDuration = 15.0
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
imagePicker.sourceType = .photoLibrary
if let availableTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary) {
if (availableTypes as NSArray).contains(type) {
/* Set up defaults */
imagePicker.mediaTypes = [type]
imagePicker.allowsEditing = canEdit
}
}
} else if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum) {
imagePicker.sourceType = .savedPhotosAlbum
if let availableTypes = UIImagePickerController.availableMediaTypes(for: .savedPhotosAlbum) {
if (availableTypes as NSArray).contains(type) {
imagePicker.mediaTypes = [type]
}
}
} else {
return
}
imagePicker.allowsEditing = canEdit
imagePicker.delegate = delegate
target.present(imagePicker, animated: true, completion: nil) // presents the imagepicker to the user
return
}
}
现在,您可以使用此类来访问照片库的快捷方式。下面提供一个示例。
import Foundation
import UIKit
class Example: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func some_action() {
let camera = Camera(source: self)
camera.PresentPhotoInput(target: self)
//camera.PresentPhotoCamera(target: self, canEdit: false)
}
// MARK: - Image Picker Delegate Function
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
if picker.sourceType == .photoLibrary {
if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
// do something
} else if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
// do something
}
picker.dismiss(animated: true, completion: nil)
} else if picker.sourceType == .camera {
// do something
picker.dismiss(animated: true, completion: nil)
}
}
}
希望这会有所帮助!