简体   繁体   中英

ios share extension camera

I am trying to take image with camera on share extension. Is it possible to do? Currently, in my main app, I write like this.

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

It is okay in my main app but in my share extension, it show black screen for camera view. How shall I do?

You can't do it. Remember, your share extension is operating in someone else's app. It can only do a small range of things there, and using the camera is not one of them. (Even if you could do it, their app might not have permission to use the camera.) Also, your share extension does not own the screen, so it can't present the image picker.

Apple's documentation about this limitation can be found here: 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:- You can use like this

SSImagePicker.shared.ssActionSheetFor(viewRect: sender, controller: self, with: [.ssCamera,.ssGalery])
    SSImagePicker.shared.imagePickedBlock = { (selectedImage) in
        self.imgProfile?.image = selectedImage
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM