简体   繁体   English

有没有办法包含仅在 SwiftUI 预览中可用的图像资产?

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

I'm developing a CocoaPod and I'd like to include images in my target that I can use in SwiftUI previews.我正在开发一个 CocoaPod,我想在我的目标中包含我可以在 SwiftUI 预览中使用的图像。 I don't want them included in the binary or the pod when I ship it.我不希望它们在我发货时包含在二进制文件或 pod 中。 Is that possible?那可能吗?

If I don't include the asset bundle in the podspec it doesn't seem to appear under my development pods, but I don't want it in the podspec because I don't want to ship the images.如果我不在 podspec 中包含资产包,它似乎不会出现在我的开发 pod 下,但我不希望它出现在 podspec 中,因为我不想发送图像。

I'm thinking there's perhaps a way to do this by running a custom step after pod install that will copy an asset bundle from the example app to the development pod but I haven't quite figured that out yet.我认为可能有一种方法可以通过在pod install之后运行自定义步骤来实现此目的,该步骤会将资产包从示例应用程序复制到开发 pod,但我还没有完全弄清楚。

When you create SwiftUI based project it creates the following group, with Preview-only assets catalog当您创建基于 SwiftUI 的项目时,它会创建以下组,其中包含仅预览资产目录

演示

Target build settings:目标构建设置:

演示

Following @Asperi's answer, I wrote the following post_install step for my development pod.按照@Asperi 的回答,我为我的开发 pod 编写了以下 post_install 步骤。 Remember to change ${TARGET_NAME} for your target:请记住为您的目标更改 ${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