访问照片应用程序以进行共享/编辑
Posted
技术标签:
【中文标题】访问照片应用程序以进行共享/编辑【英文标题】:Accessing Photos app for sharing / editing 【发布时间】:2018-12-12 08:08:13 【问题描述】:我正在制作一个照片应用程序,它可以拍照并保存到照片库。我希望用户能够像在实际的相机应用程序中一样进入照片库以共享/编辑/删除照片。为了澄清我不想访问图像选择器来选择图像然后将其加载回我的应用程序。我想从我的应用程序中打开实际的照片应用程序,以便用户可以使用现有的内置照片应用程序功能对他们进行操作。这可能吗?它似乎可以在默认的 Apple 相机应用程序中使用,但我在其他任何地方都没有看到它。我似乎找不到任何关于此的信息,因为当我用谷歌搜索时,我不断得到有关图像选择器的信息。
现在我最好的情况如下,但你离开了我的应用
UIApplication.shared.open(URL(string:"photos-redirect://")!)
【问题讨论】:
如果你让用户从原生照片应用做事,那么你的照片应用有什么用? 您好,这是一个 AR 应用程序,使用 AR 拍照,然后我只想保存到照片库,让用户对照片做他想做的事。为了方便起见,我只想有一个照片库的快捷方式 【参考方案1】:你可以实现
UIActivityViewController
该系统提供多种标准服务,例如将项目复制到粘贴板、将内容发布到社交媒体网站、通过电子邮件或 SMS 发送项目等等。应用还可以定义自定义服务。
https://developer.apple.com/documentation/uikit/uiactivityviewcontroller
【讨论】:
【参考方案2】:创建一个新的 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)
希望这会有所帮助!
【讨论】:
以上是关于访问照片应用程序以进行共享/编辑的主要内容,如果未能解决你的问题,请参考以下文章