簡體   English   中英

有沒有辦法包含僅在 SwiftUI 預覽中可用的圖像資產?

[英]Is there a way to include an image asset that will only be available in previews for SwiftUI?

我正在開發一個 CocoaPod,我想在我的目標中包含我可以在 SwiftUI 預覽中使用的圖像。 我不希望它們在我發貨時包含在二進制文件或 pod 中。 那可能嗎?

如果我不在 podspec 中包含資產包,它似乎不會出現在我的開發 pod 下,但我不希望它出現在 podspec 中,因為我不想發送圖像。

我認為可能有一種方法可以通過在pod install之后運行自定義步驟來實現此目的,該步驟會將資產包從示例應用程序復制到開發 pod,但我還沒有完全弄清楚。

當您創建基於 SwiftUI 的項目時,它會創建以下組,其中包含僅預覽資產目錄

演示

目標構建設置:

演示

按照@Asperi 的回答,我為我的開發 pod 編寫了以下 post_install 步驟。 請記住為您的目標更改 ${TARGET_NAME}:

post_install do |installer_representation|
  project = installer_representation.pods_project
  subdir = "PreviewAssets"
  # First add the PreviewAssets folder to the build settings for the debug configuration
  project.targets.each do |target|
    if target.name == "${TARGET_NAME}"
      target.build_configurations.each do |config|
        if config.name == 'Debug'
          config.build_settings['DEVELOPMENT_ASSET_PATHS'] ||= ['${PODS_TARGET_SRCROOT}/' + subdir]
        end
      end
    end
  end
  # Second grab all the xcassets from the PreviewAssets folder and add them to the ${TARGET_NAME} group in
  # the Development Pods group
  group = project.pod_group("${TARGET_NAME}")
  references = []
  # Is there a better way to get the root of your project?
  directory = installer_representation.sandbox.root.join("../..", subdir)
  Dir.chdir(directory)
  Dir.glob("*").each do |f|
    if File.directory?(f)
      full_path = File.expand_path(f)
      references << project.add_file_reference(full_path, group)
    end
  end
  # Last add the asset catalogs to the target
  project.targets.each do |target|
    if target.name == "${TARGET_NAME}"
      target.add_file_references(references)
    end
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM