集合视图不使用自定义对象

时间:2017-03-24 15:28:41

标签: ios swift uicollectionview avfoundation uicollectionviewcell

我正在制作一个应用程序,可让您录制视频,然后将其保存到文档目录和Post对象以及缩略图图像。当我录制视频并点击“使用视频”时,我看到我的打印语句,视频已保存到文档目录和post对象。但是,当我转到我的集合视图时,它被设置为显示来自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
}
}

有人能看出出了什么问题吗?

1 个答案:

答案 0 :(得分:0)

你有单身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()
}