[英]image picker/UIImagePickerController show blank screen/NavigationController swift 5
我試圖從這個問題答案鏈接中得到答案但失敗了:(
iOS UIImagePickerController 顯示空白頁
然后我嘗試下面的代碼,但仍然顯示沒有圖像列表的空白屏幕:(我認為不要觸發委托 function 您也可以從 GitHub 檢查
https://github.com/MdAshfaqurRahmanIOS/image-picker-3
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
var imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}
@IBAction func buttonAction(_ sender: UIButton) {
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = true
present(imagePicker, animated: true)
}
}
extension ViewController: UINavigationControllerDelegate, UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey(rawValue: "UIImagePickerControllerEditedImage")] as? UIImage {
imageView.image = image
}
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
}
添加此 info.plist
關鍵 =>>>> 隱私 - 照片庫使用價值 =>> 請訪問圖片庫
控制台日志
嘗試按照我下面的編程示例,首先將您的按鈕和代表添加到您的 controller 並導入照片框架:
import UIKit
import Photos
class YourViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
lazy var takePicButton: UIButton = {
let button = UIButton()
button.backgroundColor = .red
button.setTitle("Present img picker", for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = .systemFont(ofSize: 16, weight: .semibold)
button.layer.cornerRadius = 14
button.clipsToBounds = true
button.addTarget(self, action: #selector(handlepickPicture), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
現在在 viewDidLoad 添加按鈕並設置約束:
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(takePicButton)
takePicButton.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20).isActive = true
takePicButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20).isActive = true
takePicButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -20).isActive = true
takePicButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
}
之后將你的函數設置為呈現圖像選擇器 controller:
var imagePicker = UIImagePickerController()
@objc fileprivate func handlepickPicture() {
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.view.tintColor = .white
imagePicker.mediaTypes = ["public.image"]
imagePicker.allowsEditing = true
imagePicker.navigationBar.barTintColor = .black
checkPermission()
}
檢查權限,不要忘記設置您的 info.plist 隱私使用說明(如您):
fileprivate func checkPermission() {
let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
switch photoAuthorizationStatus {
case .authorized:
print("Add Photo")
if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum){
print("Access is granted by user")
self.present(imagePicker, animated: true, completion: nil)
}
print("Access is granted by user")
case .notDetermined:
PHPhotoLibrary.requestAuthorization({
(newStatus) in
print("status is \(newStatus)")
if newStatus == PHAuthorizationStatus.authorized {
/* do stuff here */
DispatchQueue.main.async {
self.present(self.imagePicker, animated: true, completion: nil)
}
print("success")
}
})
print("It is not determined until now")
case .restricted:
// same same
print("User do not have access to photo album.")
goToSetting()
case .denied:
// same same
print("Text tapped...")
goToSetting()
print("User has denied the permission.")
case .limited:
goToSetting() // the function show an alert to enable Authorization manually
print("User has denied the permission.")
@unknown default:
fatalError()
}
}
編寫 function 手動設置授權:
fileprivate func goToSetting() {
let title = "Oooooops!"
let message = "Hi man, for use this App press Go To settings and enabled access to Pohto Gallery... Check read and write option and relaunch the App!"
let alertController = UIAlertController(title: "", message: "", preferredStyle: .alert)
let subview = (alertController.view.subviews.first?.subviews.first?.subviews.first!)! as UIView
subview.backgroundColor = UIColor(white: 0, alpha: 0.2)
alertController.setValue(NSAttributedString(string: title, attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 18, weight: .heavy),NSAttributedString.Key.foregroundColor : UIColor.red]), forKey: "attributedTitle")
alertController.setValue(NSAttributedString(string: message, attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 14, weight: .regular),NSAttributedString.Key.foregroundColor : UIColor.white]), forKey: "attributedMessage")
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive) { (_) in
print("Action cancelled")
}
let goToSettingPermission = UIAlertAction(title: "Go To setting", style: .default) { (action) in
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
}
alertController.addAction(goToSettingPermission)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: {
})
}
這是結果,在 dx 上手動授權警報,在左側選擇器 controller
如果您沒有看到預覽照片,請嘗試使用其他模擬器設備運行應用程序。
您可以通過將這些照片放入 collectionView 中來使用它,如下所示“我使用 RxSwift 制作了它:
import UIKit
import RxSwift
import Foundation
import Photos
private let reuseIdentifier = "ImageCell"
class ImagesCollectionViewController: UICollectionViewController {
//MARK: - vars
private var images = [PHAsset]()
private var selectedPhotoSubject = PublishSubject<UIImage>()
var selectedPhoto: Observable<UIImage> {
return selectedPhotoSubject.asObservable()
}
//MARK: - LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
linkPhotosLibrary()
self.title = "Pick an Image"
}
//MARK: - PhotoLibrary
private func linkPhotosLibrary() {
PHPhotoLibrary.requestAuthorization { [weak self] status in
if status == .authorized {
let assets = PHAsset.fetchAssets(with: PHAssetMediaType.image,options: nil)
assets.enumerateObjects { (object, count, stop) in
self?.images.append(object)
}
self?.images.reverse()
DispatchQueue.main.async {
self?.collectionView.reloadData()
}
}
}
}
// MARK: - UICollectionViewDataSource
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ImageCell
let asset = images[indexPath.row]
let manager = PHImageManager.default()
manager.requestImage(for: asset, targetSize: CGSize(width: 200,height: 200), contentMode: .aspectFit, options: nil) { image, _ in
DispatchQueue.main.async {
cell.imageViewOutlet.image = image
}
}
return cell
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let selectedAsset = images[indexPath.row]
PHImageManager.default().requestImage(for: selectedAsset, targetSize: CGSize(width: 500, height: 500), contentMode: .aspectFit, options: nil) { [weak self] image , info in
guard let info = info else {return}
let isDegradedImage = info["PHImageResultIsDegradedKey"] as! Bool
// as we will call this function 2 times one for the small image in the collection and one here that's why we use it as this will tell us if the image is thumbnail or original image
if !isDegradedImage {
if let image = image {
self?.selectedPhotoSubject.onNext(image)
self?.dismiss(animated: true, completion: nil)
}
}
}
}
}
或者你可以正常使用它:
var myGallery: GalleryController!
func showGalleryImages () {
self.myGallery = GalleryController()
self.myGallery.delegate = self
Config.tabsToShow = [.imageTab, .cameraTab]
Config.Camera.imageLimit = 100
self.present(self.myGallery, animated: true, completion: nil)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.