【问题标题】:Save and delete UIImages from UIImagePickerController using iOS file system从 iOS 文件系统中的 UIImagePickerController 保存和删除图像
【发布时间】: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


    【解决方案1】:

    这为您提供了字符串形式的路径,您可以使用它来保存从 UIImagePickerController 中选取的图像。

    保存图片

    let data = UIImagePNGRepresentation(pickedImage)!
    do {
      try data.write(to: URL(string: "\(self.documentDirectory())/nameImage")!, options: .atomic)
    } catch let error as NSError {
      print(error.localizedDescription)
    }
    

    获取图片

    do {
       let readData = try Data(contentsOf: URL(string: "\(self.documentDirectory())/nameImage")!)
       let retreivedImage = UIImage(data: readData)
    } catch let error as NSError {
      print(error.localizedDescription)
    }
    

    【讨论】:

    • 在哪里插入保存图像和获取图像方法?我尝试了 func imagePickerController() 和 func documentDirectory()
    • 您可以在选择图像后在 func imagePickerController() 中使用保存图像。此图像将保存在您手机上的文档中。并在 func viewDidLoad() 中使用文档中的 readImage,使用“photoArray.append(retreivedImage)”。当您第二次访问该应用时,照片将自动加载。
    猜你喜欢
    • 2018-12-24
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    • 2011-12-11
    • 2015-12-03
    • 1970-01-01
    相关资源
    最近更新 更多