【发布时间】:2015-10-16 11:30:31
【问题描述】:
我在尝试将数据访问到包含一些图像 URL 的数组时遇到问题。
每次我尝试创建我的图像时,我的应用程序永远不会停止加载,并创建一个循环,顺便说一下我的数组永远不会停止填充。
这是我的代码:
class swipe : UIViewController, KinderDelegate {
var urls = [NSURL!]()
var pictureApp: String!
func fetchdata(int: Int, completion: (()->())?) {
for repo in repo {
urls.append(NSURL(string: repo.picture_url!)!)
dump(repos)
i++
NSLog("nombre de line = %i", PicArray.count)
pictureApp = repo.picture_url
}
if let img = imageCache[pictureApp!] {
let newCard = Model()
newCard.image = img
print("image présente")
}else {
for url in urls{
if #available(iOS 8.0, *) {
let newCard = Model()
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)) {
if let data = NSData(contentsOfURL: url) {
let image = UIImage(data: data)!
newCard.image = (image: image)
dispatch_async(dispatch_get_main_queue()) {
newCard.image
}
}
}
self.data.append(newCard)
}
}
}
if self.data.count == 4 {
completion!()
return
} else {
self.fetchData(0, completion: completion)
}
}
}
如何利用数组中的 URL 来创建一些图像?
【问题讨论】:
-
不应该是
for repo in repos -
除了 Azzaknight,'repos' 来自哪里?
-
谢谢你们,实际上我不知道为什么我没有使用我的循环“repo for repos”,我尝试使用它并将我的代码更改为我更新的代码,一切正常,谢谢:)
标签: arrays swift loops url nsdata