Swift 如何保存从UIPickerController拾取的多个图像?
我使用UIImagePickerController上传到应用程序中的所有图像都会显示在应用程序中,但当我关闭应用程序时,图像会消失。我对一个没有UICollectionView的图像使用了UserDefaults,代码可以正常工作。请参阅代码,但当我使用UICollectionView时,代码不再正常工作。如果有人能帮我或给我一些建议,那就太好了Swift 如何保存从UIPickerController拾取的多个图像?,swift,uicollectionview,nsuserdefaults,Swift,Uicollectionview,Nsuserdefaults,我使用UIImagePickerController上传到应用程序中的所有图像都会显示在应用程序中,但当我关闭应用程序时,图像会消失。我对一个没有UICollectionView的图像使用了UserDefaults,代码可以正常工作。请参阅代码,但当我使用UICollectionView时,代码不再正常工作。如果有人能帮我或给我一些建议,那就太好了 import UIKit class wardrobeViewController: UIViewController, UIImagePicke
import UIKit
class wardrobeViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var allImagesCollection: UICollectionView!
var collectionViewClass = CollectionViewCell()
var theUploadedImg = UIImageView()
let userDefault = UserDefaults.standard
override func viewDidLoad() {
super.viewDidLoad()
allImagesCollection.delegate = self
allImagesCollection.dataSource = self
collectionViewClass.newImage = theUploadedImg
let imageData = userDefault.object(forKey: "thePickedImage") as? NSData
if let imageData = imageData {
let image = UIImage(data: imageData as Data)
theUploadedImg.image = image
}
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func chooseImage(_ sender: Any) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
let actionSheet = UIAlertController(title: "", message: "Choose an image from", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action: UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
imagePickerController.sourceType = UIImagePickerControllerSourceType.camera
imagePickerController.allowsEditing = false
self.present(imagePickerController, animated: true, completion: nil)
}
}))
actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action: UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
imagePickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePickerController.allowsEditing = false
self.present(imagePickerController, animated: true, completion: nil)
}
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
}
var newPickedImage = [UIImage]()
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
theUploadedImg.contentMode = .scaleToFill
theUploadedImg.image = pickedImage
newPickedImage.append(pickedImage)
let pickedImageData = UIImagePNGRepresentation(pickedImage)
userDefault.set(pickedImageData, forKey: "thePickedImage")
}
picker.dismiss(animated: true, completion: nil)
allImagesCollection.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return newPickedImage.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
cell.newImage.image = newPickedImage[indexPath.item]
cell.layer.borderColor = UIColor.lightGray.cgColor
cell.layer.borderWidth = 0.5
return cell
}
let cellsPerRow = 3
override func viewWillLayoutSubviews() {
guard let collectionView = allImagesCollection, let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else { return }
let marginsAndInsets = flowLayout.sectionInset.left + flowLayout.sectionInset.right + collectionView.safeAreaInsets.left + collectionView.safeAreaInsets.right + flowLayout.minimumInteritemSpacing * CGFloat(cellsPerRow - 1)
let itemWidth = ((collectionView.bounds.size.width - marginsAndInsets) / CGFloat(cellsPerRow)).rounded(.down)
flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
}
}
您仅将1个图像保存到UserDefault。为了保存这些图像,您必须保存所有图像,而不仅仅是覆盖一个图像。此外,在应用程序启动时,您必须获取所有图像并重新加载UICollectionView。
还有一个提示,不要将图像保存为用户默认值,请使用FileManager。不要将图像存储为用户默认值。将图像作为文件写入文件夹。以用户默认值保存它们不是一个好主意。我正在尝试使用FileManager,但是我正在上载的图片没有显示,尽管图片已保存。谢谢您的建议,我将尝试使用FileManager而不是UserDefault!