首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过添加# PHPicker来同时使用UIImagePickerController和PHPicker

如何通过添加# PHPicker来同时使用UIImagePickerController和PHPicker
EN

Stack Overflow用户
提问于 2021-03-21 13:17:51
回答 1查看 247关注 0票数 0

仅iOS 14.0支持PHPickerViewController。如果您的应用程序的部署目标为iOS 13.0及更低版本,则仍必须使用UIImagePickerController.how我可以使用任何人都可以共享的KT吗

EN

回答 1

Stack Overflow用户

发布于 2021-04-12 14:04:05

PHPicker Function IOS14

代码语言:javascript
复制
@available(iOS 14, *)
class UIViewController : PHPickerViewControllerDelegate{

func pickPhoto(limit : Int , delegate : PHPickerViewControllerDelegate){
    // new in iOS 14, we can get the asset _later_ so we don't need access up front
    do {
        // if you create the configuration with no photo library, you will not get asset identifiers
        var config = PHPickerConfiguration()
        // try it _with_ the library
        config = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared())
        config.selectionLimit = limit // default
        // what you filter out will indeed not appear in the picker
            config.filter = .any(of: [.images , .livePhotos]) // default is all three appear, no filter
        config.preferredAssetRepresentationMode = .current
        let picker = PHPickerViewController(configuration: config)
        picker.delegate = delegate
        // works okay as a popover but even better just present, it will be a normal sheet
        self.present(picker, animated: true)
       // PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self)
    }
}


 func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
        guard results.first != nil else {
            picker.dismiss(animated: true, completion: nil)
            return
        }
        
        picker.dismiss(animated: true) {
            let prov = results.first!.itemProvider

            prov.loadObject(ofClass: UIImage.self) { im, err in
                if let image = im as? UIImage {
            // do whatever action with your image here
                  }
        }
 }
}

UIImagePicker

代码语言:javascript
复制
class UIViewController : UIImagePickerControllerDelegate , UINavigationControllerDelegate{
 
   func openGallary() {
        let localUIPicker = UIImagePickerController()
        localUIPicker.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate

        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
            localUIPicker.allowsEditing = false
            localUIPicker.sourceType = UIImagePickerController.SourceType.photoLibrary
            self.present(localUIPicker, animated: true, completion: {})
        }
    }

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    
            guard let choosenLogo = info[.originalImage] as? UIImage else {
                return
            }
            // do whatever action with your image here


}   
}

如何使用

代码语言:javascript
复制
// This Function call will launch picker according to compatible iOS version

func showPhotos() {
        if #available(iOS 14, *) {
            pickPhoto(limit: 1, delegate: self)
        } else {
            // Fallback on earlier versions
            openGallary()
        }
        print("open photoLibary")
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66729088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档