swift 中的内存警告问题
issue with memory warning in swift
我已将图像保存在 png 扩展名和文件系统中的 mp4 上,当我需要显示图像时,我从文档目录中获取所有 url 并将其显示在集合中查看其工作,但我需要在集合视图上显示文档目录中的视频和图像帧我这样做了但是我收到了内存警告我不知道为什么请帮助
以下我的代码:
// 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
}
// 这是包含所有 urls 表单文档目录的数组
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
arrayOfUrl = []
if let getImages = WorkingWithFileSystem.fetchAllImagesFromDocumentDirectory(folderName: folderName) {
arrayOfUrl = getImages
}
collectionView.reloadData()
}
请在没有内存警告的情况下解决这个问题
提前致谢
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
方法中泄漏内存,同时为大图像生成缩略图尺寸视频。
我已将图像保存在 png 扩展名和文件系统中的 mp4 上,当我需要显示图像时,我从文档目录中获取所有 url 并将其显示在集合中查看其工作,但我需要在集合视图上显示文档目录中的视频和图像帧我这样做了但是我收到了内存警告我不知道为什么请帮助
以下我的代码:
// 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
}
// 这是包含所有 urls 表单文档目录的数组
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
arrayOfUrl = []
if let getImages = WorkingWithFileSystem.fetchAllImagesFromDocumentDirectory(folderName: folderName) {
arrayOfUrl = getImages
}
collectionView.reloadData()
}
请在没有内存警告的情况下解决这个问题
提前致谢
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
方法中泄漏内存,同时为大图像生成缩略图尺寸视频。