前提・実現したいこと
アプリで撮影した動画をDocumentフォルダに保存し、CollectionViewにサムネイルを表示させようとしています。
撮影した動画がDocumentフォルダに保存されているところまでは確認できたのですが、サムネイルの取得がうまくいっていない状態です。
該当のソースコード
Swift
1コード(CameraViewController) 2 3 UIKit 4import AVFoundation 5import AVKit 6 7var videoUrls = [URL]() 8 9class CameraViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 10 11 12 @IBOutlet weak var collectionView: UICollectionView! 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 17 } 18 19 override func didReceiveMemoryWarning() { 20 super.didReceiveMemoryWarning() 21 22 } 23 24 @IBOutlet weak var pictureImage: UIImageView! 25 26 @IBAction func cameraButtonAction(_ sender: Any) { 27 //カメラが利用可能か 28 if UIImagePickerController.isSourceTypeAvailable(.camera){ 29 print("カメラは利用できます") 30 //UIImagePickerControllerのインスタンスを作成 31 let ipc = UIImagePickerController() 32 ipc.sourceType = .camera 33 ipc.delegate = self 34 ipc.mediaTypes = UIImagePickerController.availableMediaTypes(for: .camera) ?? [] 35 36 // 動画を高画質で保存する 37 ipc.videoQuality = .typeHigh 38 present(ipc, animated: true, completion: nil) 39 }else{ 40 print("カメラは利用できません") 41 } 42 43 } 44 45 46 //保存ボタンタップ 47 @IBAction func SNSButtonAction(_ sender: Any) { 48 if let shareImage = pictureImage.image { 49 let shareImage = [shareImage] 50 let controller = UIActivityViewController(activityItems: shareImage, applicationActivities: nil) 51 controller.popoverPresentationController?.sourceView = view 52 present(controller, animated: true, completion: nil) 53 54 } 55 } 56 57 58 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { 59 pictureImage.image = info[.originalImage] as? UIImage 60 dismiss(animated: true, completion: nil) 61 62 // 一時フォルダに保存されたファイルのURL 63 guard let fileUrl = info[.mediaURL] as? URL else { return } 64 // DocumentフォルダのURL 65 let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! 66 do { 67 // tmpに保存された動画をDocumentに移動 68 try FileManager.default.moveItem(at: fileUrl, to: documentsDirectoryURL.appendingPathComponent(fileUrl.lastPathComponent)) 69 print("動画の保存に成功しました。") 70 } catch { 71 print("動画の保存に失敗しました。") 72 73 } 74 75 76 } 77 78 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 79 let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) 80 81 let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! 82 do { 83 // Documentから動画ファイルのURLを取得 84 videoUrls = try FileManager.default.contentsOfDirectory(at: documentDirectoryURL, includingPropertiesForKeys: nil) 85 } catch { 86 print("フォルダが空です。") 87 } 88 89 // URLから動画を取得 90 let video = AVURLAsset(url: videoUrls[indexPath.row]) 91 // 動画から静止画を切り出す 92 let generator = AVAssetImageGenerator(asset: video) 93 94 let thumbnail = try! generator.copyCGImage(at: .zero, actualTime: nil) 95 96 let thumbnailView = cell.viewWithTag(1) as! UIImageView 97 thumbnailView.image = UIImage(cgImage: thumbnail) 98 99 100 return cell 101 } 102 103 104 105}
Swift
1コード(dequeueReusableCell) 2 3import UIKit 4 5class dequeueReusableCell: UICollectionViewCell { 6 7 @IBOutlet weak var thumbnailView: UIImageView! 8 9}
試したこと
func collectionViewの部分が動いていないと考えていますが、解決の糸口をつかめないでいます。
アドバイス頂けますと幸いです。
補足
恐らくサムネイルの取得ができていないものと思います。
あなたの回答
tips
プレビュー