[iOS] PHPickerViewController เครื่องมือสำหรับหยิบรูปจาก Photo Library ของใหม่บน iOS 14+
%20-%202022-08-08%20at%2015.33.58.png)
PHPickerViewController เป็น class สำหรับเรียกใช้งาน Photo Library ซึ่งมีความสามารถเพิ่มเติมจาก UIImagePickerController ที่เป็นของเดิมที่น่าจะคุ้นเคยกันอยู่แล้ว สิ่งที่เหนือกว่าของ PHPickerViewController รองรับการเลือกหลายไฟล์ (Muli-selection) รองรับ livePhoto เป็นการทำงานแบบ async ไม่มีการขอ permission การใช้งานก็ไม่ได้ยุ่งยากมากมายไปกว่ากันซะเท่าไร import PhotosUI ตั้งค่าและเรียกใช้งาน PHPickerViewController var configuration = PHPickerConfiguration() configuration.filter = .any(of: [.images, .livePhotos, .videos]) configuration.selectionLimit = 2 let picker = PHPickerViewController(configuration: configuration) picker.delegate = self self.present(picker, animated: true) และแน่นอนว่า การ handle response จำเป็นจะต้องใช้ delegate ตัวใหม่ เราจะต้อง implement protocol PHPickerViewControllerDelegate ตัวใหม่ด้วย func picker(_ picker: PHPickerViewCo...