如何在swift中使用UIColor(patternImage:UIImage(命名为:“myimage.jpg”))设置背景时的背景颜色或图像
问题描述:
我使用以下命令设置了视图的背景颜色如何在swift中使用UIColor(patternImage:UIImage(命名为:“myimage.jpg”))设置背景时的背景颜色或图像
self.view.backgroundColor = UIColor(patternImage: UIImage(named:"myimage.jpg"))
我想再次获得图像名称“myimage.jpg”。为此,我曾尝试使用下面的命令
self.view.backgroundColor?.cgColor.pattern
它返回
Optional(<CGPattern 0x600000364e00>)
我也看了Detecting if UIColor was created with colorWithPatternImage
获取到的图案图像,但它是没有多大帮助的
答
我不认为有可能获得图像的名称。
创建一个颜色,石英2D使用CGPattern
对象作为绘图在图形上下文中绘制。
CGPattern
是一个好图像,但一个单独的对象。
如果您想要检索已创建UIColor
对象的图像名称,我建议您使用散列图(NSDictionary
)作为UIColor
对象作为关键字,并将其图案图像名称用作值。现在唯一的问题是UIColor
不符合NSCopying
协议,因此不能直接作为NSDictionary
中的密钥使用,但这可以通过使用另一个对象并符合NSCopying
协议来解决。
+0
可能你是对的。请让我等待更多答案。 –
你想从背景颜色检索图像名称? –
@PuneetSharma yes –
@PuneetSharma请参阅我的更新后的问题 –