Ios 正在刷新图像时使用activityIndicator显示的图像
我正在Udacity上从事VirtualTourist项目,我们需要在图像通过Flickr客户端刷新时显示图像。目前,我的实现是这样的:刷新只会在图像完全下载后显示。项目要求图像在下载时显示为活动指示器,并且在下载过程中显示活动指示器 我的代码如下:Ios 正在刷新图像时使用activityIndicator显示的图像,ios,swift,core-data,Ios,Swift,Core Data,我正在Udacity上从事VirtualTourist项目,我们需要在图像通过Flickr客户端刷新时显示图像。目前,我的实现是这样的:刷新只会在图像完全下载后显示。项目要求图像在下载时显示为活动指示器,并且在下载过程中显示活动指示器 我的代码如下: @IBAction func newCollectionButtonDidTap(_ sender: Any) { print("FETCHING NEW COLLECTION...") DispatchQueue
@IBAction func newCollectionButtonDidTap(_ sender: Any) {
print("FETCHING NEW COLLECTION...")
DispatchQueue.main.async {
self.photos.removeAll()
self.collectionView.reloadData()
print("Reload Data newColVC")
}
for items in fetchedResultsController.fetchedObjects! {
context.delete(items as! NSManagedObject)
}
loadPhotos()
}
func loadPhotos() {
newColActivityIndicator.startAnimating()
newColActivityIndicator.isHidden = false
newCollectionButton.isUserInteractionEnabled = false
newCollectionButton.alpha = 0.4
FlickrClient.sharedInstance.getPhotos(pin.coordinate.latitude as AnyObject, lon: pin.coordinate.longitude as AnyObject, { (results, error) in
if let error = error {
print(error)
} else {
if results != nil {
for result in results! {
let picture = Photo(pin: self.pin, imageData: result as NSData, context: self.context)
self.photos.append(picture)
}
do {
try self.delegate.stack?.saveContext()
} catch let error as NSError {
print("Error saving context in loadPhotos(): \(error.localizedDescription)")
}
DispatchQueue.main.async {
self.collectionView.reloadData()
self.newColActivityIndicator.stopAnimating()
self.newColActivityIndicator.isHidden = true
self.newCollectionButton.isUserInteractionEnabled = true
self.newCollectionButton.alpha = 1.0
print("Reload Data loadPhotos")
}
}
}
})
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ImageViewCell
cell.activityIndicator.startAnimating()
cell.activityIndicator.isHidden = false
let photo = photos[indexPath.row]
if let photoImage = photo.imageData {
print("PhotoImage present")
DispatchQueue.main.async {
cell.activityIndicator.stopAnimating()
cell.activityIndicator.isHidden = true
cell.imageView?.image = UIImage(data: photoImage as Data)
}
} else {
print("PhotosImage don have")
}
return cell
}
这里的一些建议非常感谢,谢谢 因此,首先,当您点击
newCollectionButton
时,您将删除存储在core data中的所有数据,以及删除在collection view中显示的所有数据。因此,此时,集合视图没有任何可显示的内容。因此,它应该是空的,不显示任何内容
现在,我认为您的loadPhotos
方法返回来自flicker的照片集合,您将其添加到本地数组和coredata中。集合视图的数据源必须使用来自photos
array的数据
删除活动指示器,然后重新加载集合视图。此时,您已经具备了显示图像所需的数据。所以cellForItemAt
从本地数组中检索数据,并创建图像并显示它。也许这个过程发生得太快了,以至于您的cell.activityIndicator.startAnimating()
&cell.activityIndicator.stopAnimating()
执行得更快
由于您的问题不清楚,&yourloadPhotos
会获取照片数据的集合,如果您希望在每个单元格中显示activityIndicator,直到下载完所有图像,您可以
- 仅删除存储在本地/核心数据中的
照片中的图像数据
- 如果每个单元格中没有图像数据,则使单元格显示活动指示器
- 重新加载数据(),以便活动指示器启动
- 调用
loadPhotos
- 下载所有照片后,清除照片阵列,将下载的照片存储在阵列中,并调用reloadData(),以便显示所有图像