【发布时间】:2020-12-15 22:51:19
【问题描述】:
我正在尝试创建一个函数,允许用户选择多个视频,然后选择一个按钮,它将所选视频发送到另一个数组。我已经有一个类似的功能来处理删除。我真的只是想重新利用我已经拥有的用于删除的代码,但我尝试过的一切都失败了。我是 Swift 新手,但有没有办法做到这一点或我应该采取更好的方法?
var videos = [PHAsset]()
var dictionarySelectedIndexPath: [IndexPath: Bool] = [:]
@objc func didDeleteButtonClicked(_ sender: UIBarButtonItem) {
var deleteNeededIndexPaths: [IndexPath] = []
for (key, value) in dictionarySelectedIndexPath {
if value {
deleteNeededIndexPaths.append(key)
}
}
for i in deleteNeededIndexPaths.sorted(by: { $0.item > $1.item }) {
videos.remove(at: i.item)
}
collectionView.deleteItems(at: deleteNeededIndexPaths)
dictionarySelectedIndexPath.removeAll()
}
func getVideos() {
let assets = PHAsset.fetchAssets(with: PHAssetMediaType.video, options: nil)
assets.enumerateObjects({ (object, count, stop) in
self.videos.append(object)
})
self.videos.reverse()
self.collectionView.reloadData()
collectionView.delegate = self
collectionView.dataSource = self
let nib = UINib(nibName: "ItemCollectionViewCell", bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: cellIdentifier)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "videoEditorSegueIdentifier" {
let otherVc = segue.destination as! VideoEditorVC
otherVc.videos = videos
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! ItemCollectionViewCell
let asset = videos[indexPath.row]
let manager = PHImageManager.default()
if cell.tag != 0 {manager.cancelImageRequest(PHImageRequestID(cell.tag))}
cell.tag = Int(manager.requestImage(for: asset, targetSize: CGSize(width: 120.0, height: 120.0), contentMode: .aspectFill, options: nil) { (result, _) in cell.imageView?.image = result
})
return cell
}
【问题讨论】:
标签: ios swift uiviewcontroller