ios分享扩展摄像头
ios share extension camera
我正尝试在共享扩展上用相机拍照。有可能吗?
目前,在我的主要应用程序中,我是这样写的。
self.pickerController = [[UIImagePickerController alloc] init];
[self.pickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
[self.pickerController setDelegate:self.delegate];
self.pickerController.showsCameraControls = NO;
//In main VC, I write like this
[self presentViewController:[ImageTakingHelper sharedInstance].pickerController animated:YES completion:nil];
在我的主应用程序中没问题,但在我的共享扩展程序中,它显示黑屏以供摄像头查看。我该怎么办?
你做不到。请记住,您的共享扩展正在其他人的应用程序中运行。它只能在那里做一小部分事情,使用相机不是其中之一。 (即使你能做到,他们的应用程序也可能没有使用相机的权限。)此外,你的共享扩展程序不拥有屏幕,因此它无法显示图像选择器。
Apple 有关此限制的文档可在此处找到:
https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionOverview.html
import UIKit
class SSImagePicker: NSObject {
static let shared = SSImagePicker()
fileprivate var thisVC: UIViewController!
var imagePickedBlock: ((UIImage) -> Void)?
//MARK: Used to Open Camera/Library
private func openCamera(){
if UIImagePickerController.isSourceTypeAvailable(.camera){
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = .camera
myPickerController.allowsEditing = false
thisVC.present(myPickerController, animated: true, completion: nil)
}
}
private func openGallery(){
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = .photoLibrary
myPickerController.allowsEditing = true
thisVC.present(myPickerController, animated: true, completion: nil)
}
}
func ssActionSheetFor(viewRect : UIButton?,controller: UIViewController,with imagePickerTypes : [SSImagePickerType]) {
guard viewRect != nil else {
return
}
thisVC = controller
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
for objImagePickerType in imagePickerTypes {
let objAction = UIAlertAction(title: objImagePickerType.rawValue, style: .default, handler: { (alert:UIAlertAction!) -> Void in
switch alert.title {
case SSImagePickerType.ssCamera.rawValue:
self.openCamera()
case SSImagePickerType.ssGalery.rawValue:
self.openGallery()
default:
self.openGallery()
}
})
actionSheet.addAction(objAction)
}
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
if let presenter = actionSheet.popoverPresentationController {
presenter.sourceView = viewRect;
presenter.sourceRect = viewRect!.bounds
presenter.permittedArrowDirections = UIPopoverArrowDirection.up;
}
thisVC.present(actionSheet, animated: true, completion: nil)
}
}
extension SSImagePicker: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
thisVC.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.editedImage] as? UIImage {
self.imagePickedBlock?(image)
}else{
if let image = info[.originalImage] as? UIImage {
self.imagePickedBlock?(image)
}else{
alertMessase(message: "Something went wrong", okAction: {})
}
}
thisVC.dismiss(animated: true, completion: nil)
}
}
enum SSImagePickerType : String {
case ssCamera = "Camera"
case ssGalery = "Gallery"
case ssVideo = "Videos"
init(){
self = .ssGalery
}
}
//MARK:- 你可以这样使用
SSImagePicker.shared.ssActionSheetFor(viewRect: sender, controller: self, with: [.ssCamera,.ssGalery])
SSImagePicker.shared.imagePickedBlock = { (selectedImage) in
self.imgProfile?.image = selectedImage
}
我正尝试在共享扩展上用相机拍照。有可能吗? 目前,在我的主要应用程序中,我是这样写的。
self.pickerController = [[UIImagePickerController alloc] init];
[self.pickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
[self.pickerController setDelegate:self.delegate];
self.pickerController.showsCameraControls = NO;
//In main VC, I write like this
[self presentViewController:[ImageTakingHelper sharedInstance].pickerController animated:YES completion:nil];
在我的主应用程序中没问题,但在我的共享扩展程序中,它显示黑屏以供摄像头查看。我该怎么办?
你做不到。请记住,您的共享扩展正在其他人的应用程序中运行。它只能在那里做一小部分事情,使用相机不是其中之一。 (即使你能做到,他们的应用程序也可能没有使用相机的权限。)此外,你的共享扩展程序不拥有屏幕,因此它无法显示图像选择器。
Apple 有关此限制的文档可在此处找到: https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionOverview.html
import UIKit
class SSImagePicker: NSObject {
static let shared = SSImagePicker()
fileprivate var thisVC: UIViewController!
var imagePickedBlock: ((UIImage) -> Void)?
//MARK: Used to Open Camera/Library
private func openCamera(){
if UIImagePickerController.isSourceTypeAvailable(.camera){
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = .camera
myPickerController.allowsEditing = false
thisVC.present(myPickerController, animated: true, completion: nil)
}
}
private func openGallery(){
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = .photoLibrary
myPickerController.allowsEditing = true
thisVC.present(myPickerController, animated: true, completion: nil)
}
}
func ssActionSheetFor(viewRect : UIButton?,controller: UIViewController,with imagePickerTypes : [SSImagePickerType]) {
guard viewRect != nil else {
return
}
thisVC = controller
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
for objImagePickerType in imagePickerTypes {
let objAction = UIAlertAction(title: objImagePickerType.rawValue, style: .default, handler: { (alert:UIAlertAction!) -> Void in
switch alert.title {
case SSImagePickerType.ssCamera.rawValue:
self.openCamera()
case SSImagePickerType.ssGalery.rawValue:
self.openGallery()
default:
self.openGallery()
}
})
actionSheet.addAction(objAction)
}
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
if let presenter = actionSheet.popoverPresentationController {
presenter.sourceView = viewRect;
presenter.sourceRect = viewRect!.bounds
presenter.permittedArrowDirections = UIPopoverArrowDirection.up;
}
thisVC.present(actionSheet, animated: true, completion: nil)
}
}
extension SSImagePicker: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
thisVC.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.editedImage] as? UIImage {
self.imagePickedBlock?(image)
}else{
if let image = info[.originalImage] as? UIImage {
self.imagePickedBlock?(image)
}else{
alertMessase(message: "Something went wrong", okAction: {})
}
}
thisVC.dismiss(animated: true, completion: nil)
}
}
enum SSImagePickerType : String {
case ssCamera = "Camera"
case ssGalery = "Gallery"
case ssVideo = "Videos"
init(){
self = .ssGalery
}
}
//MARK:- 你可以这样使用
SSImagePicker.shared.ssActionSheetFor(viewRect: sender, controller: self, with: [.ssCamera,.ssGalery])
SSImagePicker.shared.imagePickedBlock = { (selectedImage) in
self.imgProfile?.image = selectedImage
}