我运行此代码作为imageView的扩展,将图像加载到单元格。问题是图像正在加载到单元格,但所有单元格图像都是不可见或隐藏的,直到我点击其中一个图像后显示所有单元格图像!
extension UIImageView {
func downloadImageFrom(link:String, contentMode: UIViewContentMode) {
URLSession.shared.dataTask( with: NSURL(string:link)! as URL, completionHandler: {
(data, response, error) -> Void in
DispatchQueue.main.async {
self.contentMode = contentMode
if let data = data { self.image = UIImage(data: data) }
}
}).resume()
}
}
并在cellforRowAt:
cell.imageView?.downloadImageFrom (link)
答案 0 :(得分:0)
在问题上发布的信息较少我仍然可以假设以下可能是问题
- >您的单元格有UIImageView并且您正在下载图像但是在图像下载后如何通知单元格? ,
- >由于tableview重用单元格和你的imageview也将被你的代码中的tableview单元重用,你只需要下载图像数据并设置为UIImageView,这将在未来产生问题
提示:
1)使用磁盘或内存缓存,
2)在单元格下载图像时使用占位符图像
3)使用完成处理程序(闭包)在完成下载后通知您的单元格
4)您可以使用非常容易使用的第三方库,如AlamofireImage或SDWebImage
答案 1 :(得分:0)
您可以使用以下代码: -
extension UIImageView {
static let cacheData = NSCache<AnyObject, AnyObject>()
func downloadedFrom(link: String, placeHolder:String = "placeholder",isFromCache:Bool = true,isIndicator:Bool = true,isAppendBaseUrl:Bool = true) {
let placeHolderImage = UIImage.init(named: placeHolder);
self.contentMode = UIViewContentMode.scaleAspectFill
self.clipsToBounds = true;
self.image=placeHolderImage;
var strlink = ""
if isAppendBaseUrl
{
strlink = "Your base url" + link
}
else
{
strlink = link
}
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
guard let url = URL(string: strlink) else
{
return
}
if isIndicator
{
activityIndicator.center = CGPoint(x: self.bounds.size.width/2, y: self.bounds.size.height/2)
activityIndicator.color = UIColor.white
self.addSubview(activityIndicator)
activityIndicator.startAnimating()
}
if isFromCache
{
if let cachedImage = UIImageView.cacheData.object(forKey: url as AnyObject) as? UIImage
{
if isIndicator
{
activityIndicator.stopAnimating()
}
self.image = cachedImage
}
else
{
self.image = placeHolderImage
let urlStr = strlink
let url = URL(string: urlStr)
let request: URLRequest = URLRequest(url: url!)
URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
if error != nil
{
print(error!)
return
}
DispatchQueue.main.async(execute: { () -> Void in
let image = UIImage(data: data!)
if isIndicator
{
activityIndicator.stopAnimating()
activityIndicator.removeFromSuperview();
}
if (image != nil)
{
self.image = image
UIImageView.cacheData.setObject(image!, forKey: url as AnyObject)
}
})
}).resume()
}
}
else
{
self.image=placeHolderImage;
let urlStr = strlink
let url = URL(string: urlStr)
var request: URLRequest = URLRequest(url: url!)
request.setValue("xyz", forHTTPHeaderField:"DeviceId")
URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
if error != nil
{
print(error!)
return
}
DispatchQueue.main.async(execute: { () -> Void in
let image = UIImage(data: data!)
if isIndicator
{
activityIndicator.stopAnimating()
activityIndicator.removeFromSuperview();
}
if (image != nil)
{
self.image = image
UIImageView.cacheData.setObject(image!, forKey: url as AnyObject)
}
})
}).resume()}
}}
答案 2 :(得分:0)
我通过在表格单元格中添加子视图并保存图像来解决我的问题
答案 3 :(得分:0)
试试这个,它对我有用。可能忘了指定方法super.viewDidLoad()或super.viewDidAppear(true)......
PDPageContentStream contentStream = PDPageContentStream(originalDoc, page1, PDPageContentStream.AppendMode, true, true);