gpt4 book ai didi

ios - Collection View 不使用自定义对象显示单元格

转载 作者:行者123 更新时间:2023-11-30 12:34:57 25 4
gpt4 key购买 nike

我正在制作一个应用程序,可让您录制视频,然后将其与缩略图一起保存到文档目录和 Post 对象中。当我录制视频并点击“使用视频”时,我会看到打印语句,表明视频已保存到文档目录和帖子对象中。但是,当我转到我的 Collection View (设置为显示 Post 中的缩略图)时,它是空的。这是处理保存视频和附加 Post 对象的函数:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

var posts = [Post]()
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
dismiss(animated: true, completion: nil)
if mediaType == kUTTypeMovie {
var uniqueVideoID = ""
var videoURL:NSURL? = NSURL()
var uniqueID = ""

uniqueID = NSUUID().uuidString

// Get the path as URL and store the data in myVideoVarData
videoURL = info[UIImagePickerControllerMediaURL] as? URL as NSURL?
let myVideoVarData = try! Data(contentsOf: videoURL! as URL)

// Write data to temp diroctory
let tempPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let tempDocumentsDirectory: AnyObject = tempPath[0] as AnyObject
uniqueVideoID = uniqueID + "TEMPVIDEO.MOV"
let tempDataPath = tempDocumentsDirectory.appendingPathComponent(uniqueVideoID) as String
try? myVideoVarData.write(to: URL(fileURLWithPath: tempDataPath), options: [])

//Getting the time value of the movie.
let fileURL = URL(fileURLWithPath: tempDataPath)
let asset = AVAsset(url: fileURL)
let duration : CMTime = asset.duration

//Now we remove the data from the temp Document Diroctory.
do{
let fileManager = FileManager.default
try fileManager.removeItem(atPath: tempDataPath)
} catch {
//Do nothing
}

// Check to see if video is under the 18500 (:30 seconds)
if duration.value <= 18500 {

// Write the data to the Document Directory for use later on
let docPaths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let documentsDirectory: AnyObject = docPaths[0] as AnyObject
uniqueVideoID = uniqueID + "VIDEO.MOV"
let docDataPath = documentsDirectory.appendingPathComponent(uniqueVideoID) as String
try? myVideoVarData.write(to: URL(fileURLWithPath: docDataPath), options: [])
print("docDataPath under picker ",docDataPath)
print("Video saved to documents directory")

//Create a thumbnail image from the video
let assetImageGenerate = AVAssetImageGenerator(asset: asset)
assetImageGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMake(asset.duration.value / 3, asset.duration.timescale)

//Add thumbnail & video path to Post object
if let videoImage = try? assetImageGenerate.copyCGImage(at: time, actualTime: nil) {
let video = Post(pathToVideo: docDataPath, thumbnail: UIImage(cgImage: videoImage))
posts.append(video)
print("Video saved to Post object")
}
}else{
print("Video not saved")
}
}
}

如果我设置一个断点,我可以看到 Post 在保存后确实有一个值,所以我不确定为什么 CV 不显示缩略图。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "postCell", for: indexPath) as! PostCell

cell.postImage.image = posts[indexPath.row].thumbnail
cell.postImage.contentMode = UIViewContentMode.scaleAspectFill

return cell
}

发布对象:

class Post: NSObject {
var thumbnail: UIImage!
var pathToVideo: String!

init(pathToVideo: String, thumbnail: UIImage) {
self.pathToVideo = pathToVideo
self.thumbnail = thumbnail
}
}

谁能看出出了什么问题吗?

最佳答案

你有单例VideoFeedController吗?您想通过这样做来实现什么目的:

let feed = VideoFeedController()
feed.collectionView?.reloadData()

在我看来,您正在重新加载一个从未在任何地方显示的 VideoFeedController 实例。

如果您的图像拾取函数位于 VideoFeedController 中,您只需调用

self.collectionView?.reloadData()

但如果没有,那么您可以尝试覆盖 VideoFeedController 内的 viewDidAppear 并在那里重新加载 collectionView,即将其添加到 VideoFeedController

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.collectionView?.reloadData()
}

关于ios - Collection View 不使用自定义对象显示单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43003447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com