簡體   English   中英

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
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM