我在png扩展名上保存图像,在文件系统中的mp4上保存视频,当我需要显示图像时,我从文档目录中获取所有网址并将其显示在集合视图中,但是我需要显示视频帧和集合视图中的文档目录中的图像我这样做但是我得到了内存警告我不知道为什么请任何帮助
以下我的代码:
// this function for the get video frame and display it on the collection view
func generateThumnail(url : NSURL, fromTime:Float64) -> UIImage {
let asset :AVAsset = AVAsset(url: url as URL)
let assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
assetImgGenerate.requestedTimeToleranceAfter = kCMTimeZero;
assetImgGenerate.requestedTimeToleranceBefore = kCMTimeZero;
let time : CMTime = CMTimeMakeWithSeconds(fromTime, 600)
let img:CGImage = try! assetImgGenerate.copyCGImage(at: time, actualTime: nil)
let frameImg : UIImage = UIImage(cgImage: img)
return frameImg
}
//这是集合视图
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.arrayOfUrl.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCell", for: indexPath) as! ShowPhotoAlbumCell
if self.arrayOfUrl[indexPath.row].pathExtension == "mp4" {
let grabTime = 0.50
let img = self.generateThumnail(url: self.arrayOfUrl[indexPath.row] as NSURL, fromTime: Float64(grabTime))
cell.imageViewCell.image = img
cell.imageViewVideo.isHidden = false
}else{
cell.imageViewCell.image = UIImage(contentsOfFile: self.arrayOfUrl[indexPath.row].path)
cell.imageViewVideo.isHidden = true
}
return cell
}
//这是针对包含所有网址表单文档目录
的数组 override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
arrayOfUrl = []
if let getImages = WorkingWithFileSystem.fetchAllImagesFromDocumentDirectory(folderName: folderName) {
arrayOfUrl = getImages
}
collectionView.reloadData()
}
请在没有内存警告的情况下修复此问题
感谢提前
答案 0 :(得分:0)
let img = self.generateThumnail(url: self.arrayOfUrl[indexPath.row] as NSURL, fromTime: Float64(grabTime))
cell.imageViewCell.image = img
带
cell.imageViewCell.image = UIImage(named: "thumbnail")!
很抱歉,但我认为你必须尝试使用一些静态图像来缩略图,然后检查..可能generateThumnail
在集合视图cellForItemAt
方法中泄漏内存,同时生成大尺寸视频的缩略图。