我正在敲我的头。
:定位
创建包含图像键的plist。按键名称加载和图像 - 在 UIKit
上到目前为止我尝试了什么:
Plist样本:
<plist version="1.0">
<dict>
<key>frames</key>
<dict>
<key>flowerpattern_0.jpg</key>
<dict>
<key>frame</key>
<string>{{2,2},{100,100}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{0,0},{100,100}}</string>
<key>sourceSize</key>
<string>{100,100}</string>
</dict>
</dict>
<key>metadata</key>
<dict>
<key>format</key>
<integer>2</integer>
<key>realTextureFileName</key>
<string>flower_pattern_plist.jpg</string>
<key>size</key>
<string>{1022,104}</string>
<key>smartupdate</key>
<string>$TexturePacker:SmartUpdate:2e0802c1c0d11825873827deec34bf23:622a62cd100e0ea2e4b15f2b6ecde20c:f7e873e0cfa3c1fd5648760bfa48db16$</string>
<key>textureFileName</key>
<string>flower_pattern_plist.jpg</string>
</dict>
</dict>
</plist>
我是如何尝试取出图像的:
var myDict : NSDictionary?
var temp = String()
if let path = NSBundle.mainBundle().pathForResource("flower_pattern_plist", ofType: "plist") {
myDict = NSDictionary(contentsOfFile: path)
}
if let dict = myDict {
let image: AnyObject? = dict["frames"]!
println(image)
temp = image!.allKeys[0] as! String
}
self.CollageChild.backgroundColor = UIColor(patternImage: UIImage(named: temp)!)
但到目前为止它失败了,有什么建议吗?它甚至可能吗?
答案 0 :(得分:1)
您的代码读错文件。首先,您可以将frames
定义为数组(不是字典)并使用AnyObject
,也可以使用frames["flowerpattern_0"]
。我建议使用数组选项......
接下来,在.plist文件中向对象添加fileName
键:
<key>frames</key>
<array>
<dict>
<key>fileName</key>
<string>flowerpattern_0.jpg</string>
//your other stuff...
</dict>
</array>
让你的形象像这样:
var imageName = image!.valueForKey("fileName") as! String
var finalImage = UIImage(named : imageName)