资产目录与文件夹参考:何时使用一个或另一个? [英] Asset catalog vs folder reference: When to use one or the other?

查看:123
本文介绍了资产目录与文件夹参考:何时使用一个或另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将文件放入Assets.xcassets,也可以将文件放入文件夹引用(蓝色文件夹)中.我什么时候可以选择一个?

I can put files into Assets.xcassets or I can put files into folder references (the blue folders). When would I choose one over the other?

推荐答案

您可能应该使用资产目录,因为这是Apple一直希望您使用的资产(工具会反映出这一点),并且它们具有许多优点:

You should probably use asset catalogs as that's what Apple wants you to use going forward (the tools will reflect that) and they bring many advantages:

  • 应用程序精简

  • App thinning

无需代码即可设置资产属性,例如渲染模式或切片

Setting asset properties without code, e.g., rendering mode or slicing

您不必记住@2x~ipad-568等命名约定即可自动获取设备专用资产

You don't have to remember naming conventions like @2x, ~ipad, -568 etc to get device-specific assets automatically

如果您在支持的版本和设备的正确方框中打勾,资产目录将指出丢失的资产,并且它们提供了很好的概述

Asset catalogs will point out missing assets if you tick the right boxes for the versions and devices you support, and they provide a nice overview

您应该习惯它们,因为某些平台(例如watchOS)要求您使用资产目录

You should get used to them as some platforms (e.g., watchOS) require you to use asset catalogs

有一些警告:

  • 如果您部署回iOS 6,则某些功能不要不能按预期工作 –资产目录仍然可以帮助您组织资产,但是运行时功能将无法使用,因为Xcode只会将纯图像文件转储到您的捆绑软件中.

  • If you deploy back to iOS 6, some features don't work as expected – asset catalogs still help to organize your assets, but the runtime features won't work as Xcode will just dump plain image files into your bundle.

如果您部署到iOS 7或更高版本,Xcode会将所有资产编译到一个.car文件中(这就是整个想法).但是,这很难调试,因为您无法查看已编译的文件,这也意味着您无法简单地从单个资产中获取文件URL.要创建文件URL,您始终必须加载资产(按其名称)并将其首先写入磁盘. *

If you deploy to iOS 7 or later, Xcode will compile all assets into one .car file (that's the whole idea). However, this can be harder to debug because you cannot look into the compiled file, and it also means you cannot simply get a file URL from a single asset. To create a file URL, you always have to load the asset (by its name) and write it to disk first. *

最后一点还意味着您不能使用NSBundle(在Swift 3.0:Bundle中)API来检索URL或图像文件的路径.为了从主捆绑包以外的捆绑包中加载资产,您需要依靠Apple提供一个自iOS 8.0以来的API,.如果您在资源包中组织共享代码并部署到iOS 7或更早版本,则不应使用资产目录.如果您打算开发一个框架,这可能最相关.

The last point also implies that you cannot use the NSBundle (in Swift 3.0: Bundle) APIs to retrieve URLs or paths to image files. In order to load assets from a bundle other than the main bundle, you rely on Apple to provide an API, which they do since iOS 8.0. If you organize shared code in resource bundles and deploy to iOS 7 or earlier, you shouldn't use asset catalogs. This is probably mostly relevant if you intend to develop a framework.

*例如,CoreSpotlight API允许您设置 thumbnailData 属性.如果您有一个文件URL开头,则无需将资产加载到内存中.我不确定Spotlight是否可以从您的应用程序包中访问文件URL.这只是一个例子.

* E.g., the CoreSpotlight API allows you to set a thumbnailURL, but if your image is within an asset catalog, you must either write it to disk separately yourself, or use the thumbnailData property. If you had a file URL to begin with, you'd never have to load the asset into memory. I'm not sure if Spotlight could access file URLs from within your app bundle. It's just an example.

这篇关于资产目录与文件夹参考:何时使用一个或另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆