2
비디오 URL에 묶음에서 미리보기 이미지를로드하는 데모를 만들었습니다. 이제로드 된 모든 미리보기 이미지를 내 tableview 목록에 표시하고 싶지만 문제가 있습니다. tablview 모든 축소판 이미지를 다운로드 할 때까지 내 tableview 잘 (스크롤링) 비동기 스레드를 사용하지만 20-25 초 후에 모든 축소판 이미지를로드 할 교수형있어.비디오 뷰어의 미리보기 이미지를 테이블 뷰 목록에로드하는 방법 비동기식
여기 SDWebImages처럼 나에게 뭔가를 내가 원하는 가능한 솔루션을 제공하십시오 내 코드
@IBOutlet weak var tableviewList:UITableView!
var listArray:[String] = ["https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4",
"http://techslides.com/demos/sample-videos/small.mp4",
"http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v",
"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4",
"http://techslides.com/demos/sample-videos/small.mp4",
"http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v",
"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4",
"http://techslides.com/demos/sample-videos/small.mp4",
"http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v"
]
override func viewDidLoad() {
super.viewDidLoad()
self.tableviewList.register(UINib(nibName: "ListTableViewCell", bundle: nil), forCellReuseIdentifier: "ListTableViewCell")
for aryString in self.listArray
{
self.createThumbnailOfVideoFromFileURL(aryString)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.listArray.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "ListTableViewCell", for: indexPath) as! ListTableViewCell
let strUrl = self.listArray[indexPath.row]
DispatchQueue.global(qos: .userInitiated).async {
// Download file or perform expensive task
cell.imageviewThumb?.image = self.getThumbnailFrom(path: URL.init(string:strUrl)!)
DispatchQueue.main.async {
// Update the UI
}
}
cell.selectionStyle = UITableViewCellSelectionStyle.none
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let stringUrl:String = self.listArray[indexPath.row]
let videoUrl = NSURL(string:stringUrl)
let player = AVPlayer(url:videoUrl as! URL)
let playerviewController = AVPlayerViewController()
playerviewController.player = player
self.present(playerviewController, animated: true, completion: {
playerviewController.player?.play()
})
}
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 300
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == UITableViewCellEditingStyle.delete) {
// handle delete (by removing the data from your array and updating the tableview)
}
}
func getThumbnailFrom(path: URL) -> UIImage? {
do {
let asset = AVURLAsset(url: path , options: nil)
let imgGenerator = AVAssetImageGenerator(asset: asset)
imgGenerator.appliesPreferredTrackTransform = true
let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(0, 1), actualTime: nil)
let thumbnail = UIImage(cgImage: cgImage)
return thumbnail
} catch let error {
print("*** Error generating thumbnail: \(error.localizedDescription)")
return nil
}
}
이지만하지 비디오 썸네일에 대한 이미지 만에 유용합니다. 나는 빠른 3.0 솔루션 .Thanks 사전에
감사를 시도하려는, 나는 같은 방식으로 감사를 수행 한 – user7018875