资源文件夹中的HEIC图像未加载

时间:2022-05-16 00:27:04

I have set .heic images in my xcassets folder from my project to save some space. However I'm not able to load them through the UIImage(named:) constructor. I'm always getting nil, so the only way I have to load them is by specifying the URL. Do you have any idea why?

我已经从我的项目中的xcassets文件夹中设置了.heic图像以节省一些空间。但是我无法通过UIImage(命名为:)构造函数加载它们。我总是为零,所以我必须加载它们的唯一方法是指定URL。你知道为什么吗?

imageView.image = UIImage(named: "stone") // This returns nil.

资源文件夹中的HEIC图像未加载

Again, if I add them to the project as files and I access them through this method I created everything is fine (or using libraries like SDWebImage), but I believe I'm losing the power of app thinning as the images are hosted as files, so e.g. iPhone 7 will have both 2x and 3x resolutions when only 2x is needed.

再次,如果我将它们作为文件添加到项目中并通过此方法访问它们,我创建的一切都很好(或使用像SDWebImage这样的库),但我相信我正在失去应用程序稀疏的能力,因为图像作为文件托管所以,例如当需要2倍时,iPhone 7将具有2倍和3倍分辨率。

extension UIImage {

    convenience init?(heicName: String, useScale: Bool = false) {
        guard let resourcePath = Bundle.main.path(forResource: heicName.fileName(fileExtension: .heic, useScale: useScale), ofType: nil) else {
            return nil
        }

        let url = URL(fileURLWithPath: resourcePath) as CFURL

        guard let source = CGImageSourceCreateWithURL(url, nil),
            let image = CGImageSourceCreateImageAtIndex(source, 0, nil) else {
                return nil
        }

        self.init(cgImage: image)
    }

}

So my question is, can anyone explain me the process to use HEIC as images hosted in the image assets folder and make it compatible with app thinning (just fetch the needed resources, i.e. 2x, 3x)?

所以我的问题是,任何人都可以向我解释使用HEIC作为图像资源文件夹中托管的图像的过程,并使其与app thinning兼容(只需获取所需的资源,即2x,3x)?

UPDATE: After researching more I realised images are loaded from IB when I just set the image name on the image view. Do you know how this is internally working? which constructor is IB using to set the UIImage to the UIImageView?

更新:在研究了更多之后,当我在图像视图上设置图像名称时,我意识到图像是从IB加载的。你知道这是如何在内部工作的吗?使用哪个构造函数将UIImage设置为UIImageView?

UPDATE 2: I have submitted a radar to Apple: Radar

更新2:我已向Apple提交雷达:雷达

2 个解决方案

#1


1  

Apple's asset compiler re-encodes all images that come into it in a variety of internal formats, some standard and some proprietary, depending on a bunch of factors, including the type of image, the target OS version, and possibly other factors. Unfortunately, at the time of this writing, the asset compiler seems to be buggy as far as HEIC files are concerned; if I try to put an HEIC file into an asset catalog, it appears to be left out. The image data is not copied into the Assets.car file, which itself is only 16 KB in size, containing only a few empty B-trees. Therefore, when you try to load the image using UIImage, you get nothing.

Apple的资产编译器重新编码以各种内部格式(一些是标准格式和一些专有格式)进入它的所有图像,具体取决于一系列因素,包括图像类型,目标操作系统版本以及可能的其他因素。不幸的是,在撰写本文时,就HEIC文件而言,资产编译器似乎是错误的;如果我尝试将HEIC文件放入资产目录中,它似乎被省略了。图像数据不会复制到Assets.car文件中,该文件本身大小只有16 KB,只包含几个空B树。因此,当您尝试使用UIImage加载图像时,您什么也得不到。

Fortunately, since the asset compiler recodes all images fed into it anyway, you can just re-encode your images to another format (I recommend PNG, as it is lossless), and the resulting file that the end-user receives should be identical to what it would have been with the HEIF input file (and ironically, it might end up being coded as HEIF, since the assets format supports it). So that is what I would do until Apple fixes this bug in the asset compiler.

幸运的是,由于资产编译器无论如何都会重新编码输入的所有图像,您只需将图像重新编码为另一种格式(我建议使用PNG,因为它是无损的),最终用户收到的结果文件应该与它与HEIF输入文件一样(具有讽刺意味的是,它可能最终被编码为HEIF,因为资产格式支持它)。所以,在Apple修复资产编译器中的这个错误之前,我会这样做。

#2


0  

After a big research through Radar and Apple DTS, I received the following answer from an engineer who contacted other engineering teams:

在通过Radar和Apple DTS进行了大量研究之后,我从一位联系其他工程团队的工程师那里得到了以下答案:

“Hi,

“嗨,

My email started a long email thread involving a number of engineers tracing your question through the system. Eventually, they found that HEIC format is not supported in image assets and in other parts of the system at this time where jpg and png files are presently used. Specifically, I am referring to automatically loaded resources such as those saved in storyboards or other Xcode generated resources and files. There is no workaround available at this time.

我的电子邮件启动了一个很长的电子邮件帖子,涉及许多工程师通过系统跟踪您的问题。最终,他们发现目前使用jpg和png文件的图像资产和系统的其他部分不支持HEIC格式。具体来说,我指的是自动加载的资源,例如保存在故事板或其他Xcode生成的资源和文件中的资源。目前没有可用的解决方法。

This does not mean that you cannot use HEIC images in your app. If you would like use an HEIC image in your code, then you can use Image I/O to load the image.

这并不意味着您无法在应用中使用HEIC图像。如果您想在代码中使用HEIC图像,则可以使用Image I / O加载图像。

Please try again in future versions of the operating system.”

请在将来的操作系统版本中再试一次。“

TL;DR:

TL; DR:

It’s not possible to use HEIF on the assets folder yet. Maybe in the future.

目前还无法在资源文件夹中使用HEIF。也许在将来。

#1


1  

Apple's asset compiler re-encodes all images that come into it in a variety of internal formats, some standard and some proprietary, depending on a bunch of factors, including the type of image, the target OS version, and possibly other factors. Unfortunately, at the time of this writing, the asset compiler seems to be buggy as far as HEIC files are concerned; if I try to put an HEIC file into an asset catalog, it appears to be left out. The image data is not copied into the Assets.car file, which itself is only 16 KB in size, containing only a few empty B-trees. Therefore, when you try to load the image using UIImage, you get nothing.

Apple的资产编译器重新编码以各种内部格式(一些是标准格式和一些专有格式)进入它的所有图像,具体取决于一系列因素,包括图像类型,目标操作系统版本以及可能的其他因素。不幸的是,在撰写本文时,就HEIC文件而言,资产编译器似乎是错误的;如果我尝试将HEIC文件放入资产目录中,它似乎被省略了。图像数据不会复制到Assets.car文件中,该文件本身大小只有16 KB,只包含几个空B树。因此,当您尝试使用UIImage加载图像时,您什么也得不到。

Fortunately, since the asset compiler recodes all images fed into it anyway, you can just re-encode your images to another format (I recommend PNG, as it is lossless), and the resulting file that the end-user receives should be identical to what it would have been with the HEIF input file (and ironically, it might end up being coded as HEIF, since the assets format supports it). So that is what I would do until Apple fixes this bug in the asset compiler.

幸运的是,由于资产编译器无论如何都会重新编码输入的所有图像,您只需将图像重新编码为另一种格式(我建议使用PNG,因为它是无损的),最终用户收到的结果文件应该与它与HEIF输入文件一样(具有讽刺意味的是,它可能最终被编码为HEIF,因为资产格式支持它)。所以,在Apple修复资产编译器中的这个错误之前,我会这样做。

#2


0  

After a big research through Radar and Apple DTS, I received the following answer from an engineer who contacted other engineering teams:

在通过Radar和Apple DTS进行了大量研究之后,我从一位联系其他工程团队的工程师那里得到了以下答案:

“Hi,

“嗨,

My email started a long email thread involving a number of engineers tracing your question through the system. Eventually, they found that HEIC format is not supported in image assets and in other parts of the system at this time where jpg and png files are presently used. Specifically, I am referring to automatically loaded resources such as those saved in storyboards or other Xcode generated resources and files. There is no workaround available at this time.

我的电子邮件启动了一个很长的电子邮件帖子,涉及许多工程师通过系统跟踪您的问题。最终,他们发现目前使用jpg和png文件的图像资产和系统的其他部分不支持HEIC格式。具体来说,我指的是自动加载的资源,例如保存在故事板或其他Xcode生成的资源和文件中的资源。目前没有可用的解决方法。

This does not mean that you cannot use HEIC images in your app. If you would like use an HEIC image in your code, then you can use Image I/O to load the image.

这并不意味着您无法在应用中使用HEIC图像。如果您想在代码中使用HEIC图像,则可以使用Image I / O加载图像。

Please try again in future versions of the operating system.”

请在将来的操作系统版本中再试一次。“

TL;DR:

TL; DR:

It’s not possible to use HEIF on the assets folder yet. Maybe in the future.

目前还无法在资源文件夹中使用HEIF。也许在将来。