【发布时间】:2020-07-24 00:24:16
【问题描述】:
我有一个使用 UIImagePicker 的 UICollectionView。我希望文件系统在“文档”中创建一个文件夹并保存/删除其中的 UIImages。 photoArray 目前是 UICollectionView 用来添加新 UIImages 的。 (我研究了 Apple 文件系统的文档,但很难掌握)
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var editBarButton: UIBarButtonItem!
let imagePicker = UIImagePickerController()
var photoArray: [UIImage] = []
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
imagePicker.delegate = self
}
// Button used to activate the UIImagePickerController
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
photoArray.append(pickedImage)
picker.dismiss(animated: true, completion: nil)
collectionView.reloadData()
}
}
// MARK: - collectionView DataSource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return photoArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! Cell
cell.photoImageView.image = photoArray[indexPath.row]
return cell
}
我在网上找到了这段代码,但我不知道“return documentDirectory[0]”输出什么。
func documentDirectory() -> String {
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory,
.userDomainMask, true)
return documentDirectory[0]
}
【问题讨论】:
标签: swift filesystems uiimagepickercontroller local swift5.1