繁体   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