简体   繁体   English

如何使用Xcode的xcassets切片功能在UIImageView中正确显示图像?

[英]How can I use Xcode's xcassets slicing feature to correctly display an image in a UIImageView?

I would like to use the slicing feature (available to deployments using iOS 7 or greater) to slice the following image: 我想使用切片功能(适用于使用iOS 7或更高版本的部署)来切片以下图像:

在此处输入图片说明

Currently, I have made a new image set in xcassets, and I have added two versions of this image. 当前,我已经在xcassets中创建了一个新映像集,并且添加了该映像的两个版本。 One which is 281x36 pixels, and another which is twice the size. 一个是281x36像素,另一个是尺寸的两倍。 I then used the slicing tool to slice both of the images as follows: 然后,我使用切片工具对两个图像进行切片,如下所示:

在此处输入图片说明

I created a UIImageView and set the image to "testimage". 我创建了一个UIImageView并将图像设置为“ testimage”。 I used the "ScaleToFill" mode, and the image scaled correctly in the preview: 我使用了“ ScaleToFill”模式,并且图像在预览中正确缩放:

在此处输入图片说明

Next, I ran the code to see how it would look with the iphone 6 simulator, and I am not getting the expected result: 接下来,我运行代码以查看iphone 6模拟器的外观,但没有得到预期的结果:

在此处输入图片说明

It seems as though the slicing feature is not being used when actually run on the device itself. 实际在设备本身上运行时,似乎未使用切片功能。 I have played around for a while and I've made sure the deployment target is > 7.0 (It's set to 8.4 for now). 我已经玩了一段时间,并且确保部署目标是> 7.0(目前设置为8.4)。 I checked the simulator as well, just to make sure it was simulating with 8.4, which it is. 我还检查了模拟器,以确保它是使用8.4进行模拟的。

Does anyone know what I'm doing wrong here? 有人知道我在做什么错吗?

Thank You in advance! 先感谢您!

Ok, so after quite a bit of confusion I believe I found the solution to my problem: 好的,所以经过一番困惑之后,我相信我找到了解决问题的方法:

I was importing and slicing JPG files, and I was getting the unexpected behavior I explained above. 我正在导入和切片JPG文件,并且得到了上面解释的意外行为。 I decided to try converting the same images to PNG files first, and then try slicing. 我决定先尝试将相同的图像转换为PNG文件,然后尝试切片。 Everything worked correctly! 一切正常!

I'm not sure why the JPG files are not slicing correctly, perhaps it's a bug? 我不确定为什么JPG文件没有正确切片,这可能是一个错误? Or maybe I am just missing something.. Either way, if anyone else has this issue, I suggest a simple solution is to convert your files to PNGs before importing into the image sets. 或者,也许我只是缺少一些东西。无论哪种方式,如果其他人遇到此问题,我建议一个简单的解决方案是在导入图像集之前将文件转换为PNG。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM