【问题标题】:ios share extension cameraios共享扩展摄像头
【发布时间】:2015-10-30 03:36:36
【问题描述】:

我正在尝试在共享扩展上使用相机拍摄图像。有可能吗? 目前,在我的主应用程序中,我是这样写的。

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];

在我的主应用程序中没问题,但在我的共享扩展程序中,它显示黑屏以供相机查看。我该怎么办?

【问题讨论】:

    标签: ios ios8-share-extension


    【解决方案1】:

    你做不到。请记住,您的共享扩展程序正在其他人的应用程序中运行。它在那里只能做一小部分事情,使用相机不是其中之一。 (即使您可以这样做,他们的应用也可能没有使用相机的权限。)此外,您的共享扩展程序不拥有屏幕,因此它无法显示图像选择器。

    可以在此处找到有关此限制的 Apple 文档: https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionOverview.html

    【讨论】:

    • 虽然此答案的相机访问方面是正确的,但可以在其他应用程序的上下文中使用 UIImagePickerController 访问照片。当我在 iOS 11 上进行测试时,它会在扩展应用程序的上下文中显示图像选择器并显示安全警报。所以看起来权限是从扩展的父级继承的。这甚至在没有照片访问权限的应用中展示共享扩展时也有效。
    • @meagar 点,没问题。
    【解决方案2】:
    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
        }
    

    【讨论】:

      猜你喜欢
      • 2016-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多