I have a problem choosing multiple images from the photo gallery using PhotoUI.
If I pick 6 photos in UICollectionView it's showing 5 photos.
Logs:
Filename: IMG_0001
File Address: <PUPhotosFileProviderItemProvider: 0x6000038b5c00> {types = (
"public.jpeg"
)}
2023-08-21 11:18:11.204644+0600 PhotoCollection[66458:2314832] [claims] Upload preparation for claim 1BD1CC9C-9E04-4269-ABC8-5AC861F202E3 completed with error: Error Domain=NSCocoaErrorDomain Code=260 "The file “version=1&uuid=CC95F08C-88C3-4012-9D6D-64A413D254B3&mode=compatible.jpeg” couldn’t be opened because there is no such file." UserInfo={NSURL=file:///Users/macbook/Library/Developer/CoreSimulator/Devices/276CD0A1-6FD0-4F01-ADD7-ED84023C8482/data/Containers/Shared/AppGroup/1B7CC31D-6A9C-4C80-B329-B7E6C132CC81/File%20Provider%20Storage/photospicker/version=1&uuid=CC95F08C-88C3-4012-9D6D-64A413D254B3&mode=compatible.jpeg, NSFilePath=/Users/macbook/Library/Developer/CoreSimulator/Devices/276CD0A1-6FD0-4F01-ADD7-ED84023C8482/data/Containers/Shared/AppGroup/1B7CC31D-6A9C-4C80-B329-B7E6C132CC81/File Provider Storage/photospicker/version=1&uuid=CC95F08C-88C3-4012-9D6D-64A413D254B3&mode=compatible.jpeg, NSUnderlyingError=0x600001830d20 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
Filename: IMG_0002
File Address: <PUPhotosFileProviderItemProvider: 0x6000038b5800> {types = (
"public.jpeg"
)}
Filename: IMG_0003
File Address: <PUPhotosFileProviderItemProvider: 0x6000038b5780> {types = (
"public.jpeg"
)}
Filename: IMG_0004
File Address: <PUPhotosFileProviderItemProvider: 0x6000038b5980> {types = (
"public.jpeg"
)}
Filename: IMG_0005
File Address: <PUPhotosFileProviderItemProvider: 0x6000038b5680> {types = (
"public.jpeg"
)}
Codes:
import UIKit
import PhotosUI
class HomeVC: UIViewController {
@IBOutlet weak var addFilesBarButtonItem: UIBarButtonItem!
@IBOutlet weak var fileViewCollectionView: UICollectionView!
private let cellIdentifier: String = "FilesViewCollectionViewCell"
var importedMedia: [UIImage] = []
var totalSelectedItems: Int = 0
var progressLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
fileViewCollectionView.delegate = self
fileViewCollectionView.dataSource = self
self.fileViewCollectionView.register(UINib(nibName: "FilesViewCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: cellIdentifier)
progressLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 30))
let leftBarButton = UIBarButtonItem(customView: progressLabel)
navigationItem.leftBarButtonItems = [leftBarButton]
progressLabel.isHidden = true // Hide initially
}
// MARK: - Function
// Update progress label's text
func updateProgressLabel(_ text: String) {
DispatchQueue.main.async {
self.progressLabel.isHidden = false
self.progressLabel.text = text
self.navigationController?.navigationBar.layoutIfNeeded()
}
}
// Hide progress label
func hideProgressLabel() {
DispatchQueue.main.async {
self.progressLabel.isHidden = true
self.navigationItem.leftBarButtonItems = nil
self.navigationController?.navigationBar.layoutIfNeeded()
}
}
// MARK: - IBAction
@IBAction func addFileBarButtonItemAction(_ sender: UIBarButtonItem) {
var configuration = PHPickerConfiguration()
configuration.selectionLimit = 0
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = self
present(picker, animated: true, completion: nil)
}
}
// MARK: - UICollectionViewDataSource, UICollectionViewDelegate
extension HomeVC: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return importedMedia.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as? FilesViewCollectionViewCell else {
return UICollectionViewCell()
}
cell.filePhotoView.image = importedMedia[indexPath.row]
return cell
}
}
// MARK: - UICollectionViewDelegateFlowLayout
extension HomeVC: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
cell.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
UIView.animate(withDuration: 0.4, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.8, options: .curveEaseOut, animations: {
cell.transform = .identity
}, completion: nil)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = (fileViewCollectionView.frame.size.width / 4) - 1
let height = (fileViewCollectionView.frame.size.height / 8) - 1
return CGSize(width: width, height: height)
}
}
// MARK: - PHPickerViewControllerDelegate
extension HomeVC: PHPickerViewControllerDelegate {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
dismiss(animated: true)
for result in results {
result.itemProvider.loadObject(ofClass: UIImage.self) { object, error in
if let image = object as? UIImage {
self.importedMedia.append(image)
if let url = result.itemProvider.suggestedName {
print("Filename: \(url)")
print("File Address: \(result.itemProvider)")
}
}
DispatchQueue.main.async {
self.fileViewCollectionView.reloadData()
}
}
}
}
}
I am working on the Photo Editor app. Try Pick and Show multiple image or videos in UICollectionView
if I pick 6 photos in UICollectionView it's showing 5 photo.