【发布时间】:2016-08-22 04:51:50
【问题描述】:
我正在尝试使用 Swift 在 XCode 中的 UIButton 中放入图像。该按钮位于键盘扩展中,我正在尝试放入“地球”图标,就像 iOS 内置键盘中已有的一样。我曾经使用地球 emoji 和 unicode(它会自动被选为 emoji),但这看起来不太好。
所以现在,无论我使用哪个文件,我的图像都是模糊的。到目前为止,我使用了 22pt、200pt、600pt、svg 和 pdf 的图像。没有运气。
这是它的样子: Blurry image
这就是 Apple 的外观,十分锐利:Sharp image
我的按钮代码是这样的:
//Set an image to fit inside a button
let btn = nextKeyboardButton
let image = UIImage(named: "globe")!
var scaledImage = image
var targetWidth : CGFloat = 0
var targetHeight : CGFloat = 0
let spacing : CGFloat = 10
if(btn.frame.width < btn.frame.height){
targetWidth = round(btn.frame.width - (spacing * 2))
targetHeight = targetWidth
} else {
targetHeight = round(btn.frame.height - (spacing * 2))
targetWidth = targetHeight
}
UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight))
image.drawInRect(CGRectMake(0, 0, targetWidth, targetHeight))
scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
btn.setImage(scaledImage, forState: .Normal)
btn.setTitle("", forState: .Normal)
btn.tintColor = UIColor.blackColor()
由于图标是 1:1,我使用相同的宽度和高度。但是对于另一个图标/图像,我也需要相同的(工作)方法。我在 25、50 和 75pts 中为标签栏使用的任何其他图标/图像看起来都很好且清晰。我使用哪个模拟器也没关系,如果是 iPhone 4s 或 6 Plus。
另外,还有其他解决方案吗?我尝试使用另一种全是地球仪的字体,但 XCode 不会在编辑器中列出它。我也尝试过在全球范围内绘制表情符号,使用 unicode 字符,但它会被当作表情符号......所以.. 有什么帮助吗? :)
【问题讨论】:
-
我知道这听起来很琐碎,但是您是否以@2x 和@3x 结束了较大图像的名称?另外,您是否尝试过使用PNG?我通常使用它,它看起来很完美。您是否也尝试过以像素而不是点来保存图像?
-
有两个问题: 1. 资产文件中的矢量图像在构建时转换为 PNG 文件,因此在缩放时保证会出现一些模糊(与 PDF 是本机渲染的情况相反),以及2.
UIGraphicsBeginImageContext不使用设备缩放。你想要UIGraphicsBeginImageContextWithOptions(size, NO, 0) -
是的,我尝试了很多东西,因此记得把 2x 和 3x 正确。我什至考虑让它们变成不同的颜色,只是为了看看使用的是哪一种。我也尝试过像素和点。但我确实怀疑 UIGraphicsBeginImageContext 是这里的问题。我只是不太熟悉它。我会试一下! :)
标签: ios swift uibutton uiimage