以下是照片应用的代码。
class VideoList_TableViewController: UITableViewController {
var videoAssets:PHFetchResult<PHAsset>?
var imageManager:PHImageManager?
override func viewDidLoad() {
super.viewDidLoad()
self.imageManager = PHImageManager()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int
{
// here i am fetching assets
videoAssets = PHAsset.fetchAssets(with: .video, options:nil)
return 1
}
override func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
print((videoAssets?.count)!)
return (videoAssets?.count)!
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "videoList_Table_Cell", for: indexPath) as! videoList_TableView_Cell_Controller
if (!cell.didLoaded) {
cell.didLoaded = true
cell.asset = videoAssets?[indexPath.row]
cell.imageManager = self.imageManager
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! videoList_TableView_Cell_Controller
let playerController = AVPlayerViewController()
let player = AVPlayer(url: cell.videoUrl!)
playerController.player = player
self.present(playerController, animated: true, completion: nil)
playerController.player?.play()
}
}
videoList_TableView_Cell_Controller
class videoList_TableView_Cell_Controller: UITableViewCell {
var imageManager:PHImageManager?
var videoUrl:URL?
var didLoaded = false
var asset:PHAsset? {
didSet {
DispatchQueue.global().async {
self.imageManager?.requestAVAsset(forVideo: self.asset!, options: nil, resultHandler: { (vAsset, audio, info) in
let avUrl = vAsset as? AVURLAsset
let vurl = avUrl?.url
let imageGenerator = AVAssetImageGenerator(asset:vAsset!)
let time = CMTimeMakeWithSeconds(1.0, 1)
var actualTime : CMTime = CMTimeMake(0, 0)
let myImage = try! imageGenerator.copyCGImage(at: time, actualTime: &actualTime)
DispatchQueue.main.async {
self.videoUrl = vurl
self.videoTypeLbl.text = vurl?.pathExtension
self.videoNameLbl.text = vurl?.lastPathComponent
self.videoDurationLbl.text = String(CMTimeGetSeconds((vAsset?.duration)!))
self.imageV.image = UIImage(cgImage:myImage)
}
})
}
}
}
@IBOutlet weak var imageV: UIImageView!
@IBOutlet weak var videoTypeLbl: UILabel!
@IBOutlet weak var videoDurationLbl: UILabel!
@IBOutlet weak var videoNameLbl: UILabel!
}
这是swift3 + xcode 8.2中视频播放应用的代码。问题是,当我运行此代码时,我的应用程序占用了太多内存。我认为这是因为我正在获取所有视频资产。有没有什么好方法可以获取资产?
答案 0 :(得分:0)
您可以在TableView的cellForItemAt方法中获取有关PHAsset的详细信息。 这可能会解决您的问题
var videos: PHFetchResult<PHAsset>!
override func viewDidLoad() {
super.viewDidLoad()
getVideos()
}
在获取视频功能中,您可以重新加载TableView。
func getVideos() {
let options = PHFetchOptions()
options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: false) ]
options.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.video.rawValue)
videos = PHAsset.fetchAssets(with: options)
tableView.reloadData() // reload your TableView
}
在这里,我们调用tableView方法和委托 返回PHAsset的数量
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return videos.count
}
获取有关PHAsset的信息
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "videoList_Table_Cell", for: indexPath) as! videoList_TableView_Cell_Controller
let asset = videos!.object(at: indexPath.row)
PHImageManager.default().requestAVAsset(forVideo: asset, options: nil, resultHandler: { (vAsset, audio, info) in
let avUrl = vAsset as? AVURLAsset
let vurl = avUrl?.url
let imageGenerator = AVAssetImageGenerator(asset:vAsset!)
let time = CMTimeMakeWithSeconds(1.0, 1)
var actualTime : CMTime = CMTimeMake(0, 0)
let myImage = try! imageGenerator.copyCGImage(at: time, actualTime: &actualTime)
cell.videoUrl = vurl
cell.videoTypeLbl.text = vurl?.pathExtension
cell.videoNameLbl.text = vurl?.lastPathComponent
cell.videoDurationLbl.text = String(CMTimeGetSeconds((vAsset?.duration)!))
cell.imageV.image = UIImage(cgImage:myImage)
})
return cell
}
处理单元格点击
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! videoList_TableView_Cell_Controller
let playerController = AVPlayerViewController()
let player = AVPlayer(url: cell.videoUrl!)
playerController.player = player
self.present(playerController, animated: true, completion: nil)
playerController.player?.play()
}
}