ios分享扩展相机

时间:2015-10-30 03:36:36

标签: ios ios8-share-extension

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

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

在我的主应用程序中没关系,但是在我的共享扩展程序中,它显示黑屏用于摄像机视图。我该怎么办?

2 个答案:

答案 0 :(得分:2)

你不能这样做。请记住,您的共享扩展程序正在其他人的应用程序中运行。它只能在那里做一小部分事情,使用相机不是其中之一。 (即使你可以这样做,他们的应用可能没有使用相机的权限。)此外,你的共享扩展程序不拥有屏幕,因此它无法呈现图像选择器。放弃你的计划并考虑别的事情。更现实的东西。

答案 1 :(得分:0)

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
    }