简体   繁体   English

引用资产目录并从框架中包含资产目录都会创建重复项吗?

[英]Does Both Referencing an Asset Catalogue and Including it from a Framework Create Duplication?

As discussed here & here , the only apparent way to host re-usable images in a Framework's .xcassets file, then use those images in the storyboard / XIB of an app, is to manually create a reference to that catalog file directly within the app's project. 正如此处此处所讨论的,将可重用图像托管在Framework的.xcassets文件中,然后在应用程序的故事板/ XIB中使用这些图像的唯一明显方法是,直接在应用程序的内部手动创建对该目录文件的引用项目。 This puts the assets within the app's main bundle, and avoids this error when trying to use them via Interface Builder: 这会将资产放入应用程序的主捆绑包中,并在尝试通过Interface Builder使用资产时避免了该错误:

Could not load the "ImageName" image referenced from a nib in the bundle with identifier "BundleName" 无法加载标识符为“ BundleName”的捆绑包中笔尖引用的“ ImageName”图像

My question, though, is: Does this lead to duplication of the assets at build-time? 但是,我的问题是: 这会导致在构建时资产重复吗? Is iOS smart enough not to double-up assets which are both: iOS是否足够聪明,不会加倍两者的资产:

  • Referenced in the app's main bundle and 在应用程序的主捆绑中引用, 并且
  • Included in the bundle of an embedded framework 包含在嵌入式框架捆绑中

If this does lead to duplication, what other approach can achieve the same result more efficiently? 如果这确实导致重复,那么还有什么其他方法可以更有效地达到相同的结果? The framework needs to be embedded in the app, because it contains other reusable code, and the assets need to be in the framework's catalog, because they're also used in other apps. 该框架需要嵌入到应用程序中,因为它包含其他可重用的代码,并且资产也必须位于框架的目录中,因为它们也在其他应用程序中使用。

Apple's goal is to minimize the space required for assets, so assets are combined and compressed into one file called 'Asset.car'. 苹果的目标是最小化资产所需的空间,因此资产被合并并压缩到一个名为“ Asset.car”的文件中。

When you submit to the app store a process called App Thinning is started that creates variants for different actual devices. 当您提交给应用商店时,将启动一个名为“应用细化”的过程,该过程将为不同的实际设备创建变体。

You can mimic the process locally and with a third-party tools even take a look inside the 'Asset.car' file. 您可以在本地模拟该过程,甚至可以使用第三方工具在“ Asset.car”文件中查看。

I described the whole process in detail in this answer: Pixelated images on iOS10 when building with Xcode 10 我在此答案中详细描述了整个过程: 使用Xcode 10构建iOS10上的像素化​​图像

To test it myself I did the following: 为了自己测试,我做了以下工作:

  • I created a workspace 我创建了一个工作区
  • I added a framework 我添加了一个框架
  • I added an app 我添加了一个应用
  • inside the framework I added a 'Media.xcassets' 在框架内,我添加了一个'Media.xcassets'
  • I reference this Media.xcassets from the app 我从应用程序引用了这个Media.xcassets
  • I added one picture called 'Regensburg.jpg' to Media.xcassets 我在Media.xcassets中添加了一张名为“ Regensburg.jpg”的图片
  • I use this image in a storyboard 我在情节提要中使用此图像

Then I do the process described in my linked answer above and extract the Asset.car file. 然后,执行上面链接的答案中所述的过程,并提取Asset.car文件。

When taking a look with the third-party tool inside, one can see that the image is just once there. 当查看内部的第三方工具时,可以看到该图像仅在其中一次。

So good news: like expected no duplication happens. 好消息:像预期的那样,没有重复发生。

Screenshot 截图

截图

In the screenshot you can see: 在屏幕截图中,您可以看到:

  • Xcode project structure with framework and app as well as .xcassets in the background 具有框架和应用程序以及后台的.xcassets的Xcode项目结构

  • usage of the only image inside in the storyboard 情节提要中唯一图像的使用

  • the extracted Assets.car in Finder in the lower right area 在Finder右下角区域中提取的Assets.car

  • finally the mentioned third-party tool in the lower left area, where you see that the file is there just once 最后在左下角提到的第三方工具,在那里您看到文件仅存在一次

More information 更多信息

Like in my other answer I would recommend to take a look at this WWDC 2018 video: Session 227, Optimizing App Assets, https://developer.apple.com/videos/play/wwdc2018/227/ 就像在其他答案中一样,我建议您看一下WWDC 2018的这段视频:第227节,优化应用程序资产, https://developer.apple.com/videos/play/wwdc2018/227/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用IBDesignable Views中资产目录中的命名颜色即使在指定Bundle时也会崩溃IB - Using named colors from asset catalogue in IBDesignable Views crashes IB even when specifying the Bundle 如何在启动故事板中使用资产目录中的启动映像? - How do I use the Launch Images from an Asset Catalogue in the Launch Storyboard? 为iOS创建目录应用程序的指南 - Guidance to create a catalogue app for iOS Xcode 11,暗/亮图像的资产目录命名约定? - Xcode 11, Asset catalogue naming convention for dark / light images? 引用资产目录中文件夹中的图像 - Referencing image in a folder in asset catalog 无法从“照片”框架从视频资产的URL获取数据 - Unable to fetch data from URL of video Asset from Photos framework 如何将资产包含在框架中? - How to include asset in a framework? 如何在包括 Swift 在内的 iOS 中创建开发框架? - How Do I Create a Development Framework In iOS Including Swift? 引用块中的iOS应用程序委托是否会创建委托对象的副本? - Does referencing an iOS app delegate in a block create a copy of the delegate object? 引用单例的sharedInstance是否会创建保留周期? - Does referencing a singleton's sharedInstance create a retain cycle?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM