I've Made and Extension of UIImageView that Allows me to Asynchronously Download images and then Assign them to their ImageView According to their specific Index (IndexPath in the CollectionView). It works great, the only problem is:
我创建并扩展了UIImageView,允许我异步下载图像,然后根据它们的特定索引(集合视图中的IndexPath)将它们分配给它们的ImageView。它效果很好,唯一的问题是:
- I STILL SEE WRONG IMAGES DISPLAYED WHEN SCROLLING FAST AND STOPPING.
- 当快速滚动和停止时,我仍然会看到错误的图像。
Can anyone help me please? Here is my code:
有人能帮我吗?这是我的代码:
// UIImageView Extension:
/ / UIImageView扩展:
extension ExtendedUIImageView {
func downloadFrom(link link:String, contentMode mode: UIViewContentMode, imageIndex: Int) {
if (NSFileManager.defaultManager().fileExistsAtPath(link)) {
self.image = UIImage(contentsOfFile: link)
return
}
self.imageURL = NSURL(string: link)
let url = NSURL(string: link)
contentMode = mode
NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
guard
let httpURLResponse = response as? NSHTTPURLResponse where httpURLResponse.statusCode == 200,
let mimeType = response?.MIMEType where mimeType.hasPrefix("image"),
let data = data where error == nil,
let image = UIImage(data: data)
else { return }
dispatch_async(dispatch_get_main_queue()) { () -> Void in
if (url == self.imageURL) {
self.image = image
}
}
}).resume()
}
}
// And Id be Calling this method like this:
//我这样称呼这个方法:
self.previewImageView?.downloadFrom(link: localPath, contentMode: .ScaleAspectFill, imageIndex: cell.indexPath.item)
// Where:
/ /地点:
var previewImageView: ExtendedUIImageView?
PLEASE HELP ME :-O
请帮我:- o
1 个解决方案
#1
1
Hello~ I have been trying to do the same thing while ago. This is what I did. It was simple enough for me and working fine without any problems.
你好,我刚才一直在做同样的事情。这就是我所做的。这对我来说很简单,工作也很好,没有任何问题。
import Foundation
import UIKit
extension UIImageView {
//load image async from inaternet
func loadFromURL(photoUrl:String){
//NSURL
let url = NSURL(string: photoUrl)
//Request
let request = NSURLRequest(URL:url!);
//Session
let session = NSURLSession.sharedSession()
//Data task
let datatask = session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
if error != nil {
print(error?.localizedDescription)
}
dispatch_async(dispatch_get_main_queue()) {
self.image = UIImage(data: data!)
}
}
datatask.resume()
}
}
#1
1
Hello~ I have been trying to do the same thing while ago. This is what I did. It was simple enough for me and working fine without any problems.
你好,我刚才一直在做同样的事情。这就是我所做的。这对我来说很简单,工作也很好,没有任何问题。
import Foundation
import UIKit
extension UIImageView {
//load image async from inaternet
func loadFromURL(photoUrl:String){
//NSURL
let url = NSURL(string: photoUrl)
//Request
let request = NSURLRequest(URL:url!);
//Session
let session = NSURLSession.sharedSession()
//Data task
let datatask = session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
if error != nil {
print(error?.localizedDescription)
}
dispatch_async(dispatch_get_main_queue()) {
self.image = UIImage(data: data!)
}
}
datatask.resume()
}
}