0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

usdzファイルからサムネイルを出力する

Posted at

実はusdzファイルからサムネイル画像を作れる

ステップ1
QuickLookThumbnailingをimportする

import QuickLookThumbnailing

ドキュメント

ステップ2
usdzファイルのpathを生成

 func generateThumbnail(for resource: String,
                           withExtension: String = "usdz",
                           size: CGSize) {
        guard let url = Bundle.main.url(forResource: resource, withExtension: withExtension) else { return }
}

ステップ3 QLThumbnailGeneratorをインスタンス化(シングルトン)

let generator = QLThumbnailGenerator.shared

ステップ4 サムネイルにするためのrequestを作る

// 引数にパスとサイズ、大きさ、representationTypesを入れる。
// representationTypes-> 表示するサムネイルの画像の種類。他にiconとか色々ある。
let request = QLThumbnailGenerator.Request(fileAt: url,
                                                   size: size,
                                                   scale: scale,
                                                   representationTypes: .all)

ステップ5 実際にrequestを飛ばす。

 generator.generateRepresentations(for: request) { (thumbnail, type, error) in
            DispatchQueue.main.async {
                if thumbnail == nil || error != nil {
                    print(error?.localizedDescription ?? "")
                } else {
                    self.thumbnailImage = Image(uiImage: thumbnail!.uiImage)
                }
                
            }
        }

全体のコード

// Generatorクラス作成し、ObservableObjectに準きょさせてViewModelっぽく使ってる
class ThumbnailGenerator: ObservableObject {
    @Published var thumbnailImage: Image?
    func generateThumbnail(for resource: String,
                           withExtension: String = "usdz",
                           size: CGSize) {
        guard let url = Bundle.main.url(forResource: resource, withExtension: withExtension) else { return }
        let scale = UIScreen.main.scale
        let request = QLThumbnailGenerator.Request(fileAt: url,
                                                   size: size,
                                                   scale: scale,
                                                   representationTypes: .all)
        let generator = QLThumbnailGenerator.shared
        generator.generateRepresentations(for: request) { (thumbnail, type, error) in
            DispatchQueue.main.async {
                if thumbnail == nil || error != nil {
                    print(error?.localizedDescription ?? "")
                } else {
                    self.thumbnailImage = Image(uiImage: thumbnail!.uiImage)
                }
                
            }
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?