Swift:从 Internet 下载图像并缓存它们无法正常工作.需要建议 [英] Swift: download image from Internet and cache them doesn't work properly. need suggestions
问题描述
我是swift的新手,我正在构建一个从互联网下载图像并显示在UICollectionView中的应用程序,我可以成功实现此功能,但是,您似乎超时滚动屏幕,它再次下载了图像再次,这可能会导致用户的大量数据.我发现我可以使用缓存来解决这个问题,但它不起作用,我认为我做的一切都是正确的,但似乎数据没有存储在缓存中.这是代码,任何人都可以帮我解决这个问题吗?谢谢
I am new to swift, and I am building an App that download images from the Internet and display in a UICollectionView, I can achieve this function successfully, however, it seems like overtime you scroll the screen, it downloaded the images again and again, which may causes a lot of data of the users. I found that I can use cache to solve this problem, but it didn't work, I think I did everything right, but seems the data didn't stored in the cache. here is the code, anyone can help me solve this? thanks
var imageCache = [String: UIImage]()
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: UICollectionViewDataSource {
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell
Alamofire.request(.GET, "http://localhost:8888/wordpress/wp-json/wp/v2/posts").responseJSON { (response) in
if let jsonFile = response.result.value {
var arrayOfPosts = [Posts]()
for post in jsonFile as! NSArray {
let postObj = Posts()
postObj.postThumbnailUrlString = post.valueForKeyPath("featured_image_thumbnail_url") as! String
arrayOfPosts.append(postObj)
}
let imageUrlString: String = arrayOfPosts[indexPath.row].postThumbnailUrlString
let imageUrl: NSURL = NSURL(string: imageUrlString)!
if let imageFromCache = imageCache[imageUrlString] {
cell.imageView.image = imageFromCache
return
}
let request = NSURLRequest(URL: imageUrl)
let session = NSURLSession.sharedSession()
let dataTask = session.dataTaskWithRequest(request) { (data, response, error) in
let imageToCache = UIImage(data: data!)
imageCache[imageUrlString] = imageToCache
dispatch_async(dispatch_get_main_queue(), {
cell.imageView.image = imageToCache
})
}
dataTask.resume()
}
}
return cell
}}
我还有一个 Posts 类和一个自定义的 collectionviewcell.这个收藏了我很久,真的希望有人能帮我解决这个问题.谢谢!!
I also have a class of Posts and a customized collectionviewcell. this stocked me a long time, really hope anyone can help me solve this problem. thanks!!
推荐答案
Swift 2.2 或 2.3
在你的 pod 文件中添加 --> pod 'AlamofireImage', '~> 2.5'.
Add --> pod 'AlamofireImage', '~> 2.5' in your pod file.
斯威夫特 3.1
在你的 pod 文件中添加 --> pod 'AlamofireImage', '~> 3.3'.
Add --> pod 'AlamofireImage', '~> 3.3' in your pod file.
在你的代码中加入下面这行代码,它会自动缓存图片并下载它.
Put the following line in your code which will automatically cache the image and download it as well.
override func viewDidLoad() {
super.viewDidLoad()
//hit your web service here to get all url's
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return arrImageURL.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
cell.imgViewPhoto.af_setImageWithURL(arrImageURL[indexPath.row], placeholderImage: UIImage())
}
这篇关于Swift:从 Internet 下载图像并缓存它们无法正常工作.需要建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!