【发布时间】:2020-10-15 09:54:04
【问题描述】:
我目前有一个用于从存储在数组中的 url 下载图像的 for 循环。每获取 2 或 3 个新图像后,我总是遇到相同图像的问题。每次我获取新图像时,我都可以看到它使用不同的 url 来下载图像,但数组中的图像是相同的。
下面是我的代码:
func downloadImageDetailsFromFlickr(completion: @escaping (Bool, Error?) -> Void) {
self.imageLoading(is: true)
flickrClient.getImageDetailsFromFlickr(lat: latitude, lon: longitude) { (photo, error) in
if error == nil {
self.photoStore = []
for image in photo {
if image.url_m.count == 0 {
self.imageLabel.isHidden = false
self.imageLoading(is: false)
completion(false, nil)
} else {
self.photoStore.append(image.url_m)
print(self.photoStore)
completion(true, nil)
}
}
} else {
print(error?.localizedDescription ?? "Error in the fetch image block")
print("problem in downloading details.")
completion(false, error)
}
}
}
func downloadImage(completion: @escaping(Bool, Error?) -> Void ) {
let flickrImage = FlickrImage(context: self.dataController.viewContext)
for flickr in photoStore {
self.photoUrl = ""
self.photoUrl = flickr
}
flickrClient.getImage(imageUrl: photoUrl ?? "") { (data, error) in
if error == nil {
self.imageLoading(is: false)
flickrImage.photo = data
flickrImage.url = self.photoUrl
flickrImage.locations = self.location
flickrImage.locations?.latitude = self.latitude
flickrImage.locations?.longitude = self.longitude
}
else {
print(error?.localizedDescription ?? "Something went wrong downloading an image")
}
do {
try self.dataController.viewContext.save()
self.photoStore.removeAll()
completion(true, nil)
} catch {
print("Error saving into Core Data")
completion(false, error)
}
}
}
请忽略红色框,白色框表示正在重新获取的图像。我的猜测是,它与核心数据有关。
【问题讨论】:
-
此 for 循环
for flickr in photoStore { self.photoUrl = "" self.photoUrl = flickr }始终将 photostore 的最后一项存储在 photoUrl 中。这个循环的目的是什么? -
@luckystars 我希望它一次传入每个值。它奏效了,它消除了网格布局中重复的图像,但是在获取了几次新图像之后,你会得到相同的图像网格。
标签: ios swift core-data uicollectionview nsfetchedresultscontroller