在新集合ViewCell中加载选定的索引项(使图像缓存无效)



我在加载自定义图像视图时遇到很多问题:UIImageView在详细信息视图中基于集合视图单元格的选定索引路径控制器。我已经成功地传递并加载了UILabels和UITextViews,但无法使用相同的过程和代码逻辑从相同的selectedIndexPath加载CustomImageView:UIImageView。我相信这与清除或重置我的图像缓存有关,但不确定在哪里或确切地执行什么代码来执行此操作。很抱歉任何多余的代码,只是想彻底。感谢您的任何帮助或指导!

存储来自 Firebase 的值的模型对象类

class CurrentPlanner: SafeJsonObjectPlanner {
var name: String?
var profileImageUrl: String?
var planningPlace: String?
init(dictionary: [String: AnyObject]) {
self.name = dictionary["addedBy"] as? String
self.profileImageUrl = dictionary["profileImageUrl"] as? String
self.planningPlace = dictionary["planning"] as? String
}
}

CustomImageView:填充第一个集合ViewController 的UIImageView扩展

let imageCache = NSCache<NSString, UIImage>()
class CustomImageView: UIImageView {
var imageUrlString: String?
func loadImageUsingUrlString(_ urlString: String) {
imageUrlString = urlString
let url = URL(string: urlString)
image = nil
if let imageFromCache = imageCache.object(forKey: urlString as NSString) {
self.image = imageFromCache
return
}
URLSession.shared.dataTask(with: url!, completionHandler: { (data, respones, error) in
if error != nil {
print(error!)
return
}
DispatchQueue.main.async(execute: {
let imageToCache = UIImage(data: data!)
if self.imageUrlString == urlString {
self.image = imageToCache
}
imageCache.setObject(imageToCache!, forKey: urlString as NSString)
})
}).resume()
}
}
extension UIImageView {
func loadImageUsingCacheWithUrlString(_ urlString: String) {
self.image = nil
//check cache for image first
if let cachedImage = imageCache.object(forKey: urlString as NSString) {
self.image = cachedImage
return
}
//otherwise fire off a new download
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
//download hit an error so lets return out
if let error = error {
print(error)
return
}
DispatchQueue.main.async(execute: {
if let downloadedImage = UIImage(data: data!) {
imageCache.setObject(downloadedImage, forKey: urlString as NSString)
self.image = downloadedImage
}
})
}).resume()
}
}

在第一个集合中规划单元格类View

class BasePlanningCell: BaseCell2 {
var currentPlanners = [CurrentPlanner]()
var currentPlanner: CurrentPlanner? {
didSet {
setupProfileImage()
}
}
fileprivate func setupProfileImage() {
if let profileImageUrl = currentPlanner?.profileImageUrl {        
userProfileImageView.loadImageUsingCacheWithUrlString(profileImageUrl)
}
}

第一个集合中的点击单元格类视图 - 调用委托方法

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("cell tapped")
// cell delegate method called from collectionViewController
travelersFeedVC?.showPlanningViewDetailView(indexPath: indexPath)
}

第一个集合ViewController 类 - 在单元格点击上执行的方法

func showPlanningViewDetailView(indexPath: IndexPath) {
let plannersDetailVC = PlanningPlaceDetailsVC()
plannersDetailVC.profileImageUrl = plannedPlaces[indexPath.row].profileImageUrl!
print(plannersDetailVC.profileImageUrl!)
show(plannersDetailVC, sender: self)
}

2nd DetailViewController 类

var nameString: String!
var locationString: String!
var profileImageUrl: String!
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch indexPath.item {
case 0:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PlanningDetailViewCells", for: indexPath) as! PlanningDetailViewCells
cell.myMethod(str: nameString)
cell.getLocationMethod(str: locationString)
cell.getProfileImageMethod(str: profileImageUrl)
return cell
case 1:
// ...
case 2:
// ...
default:
// ...
}
}

2nd DetailView控制器单元代表类

func myMethod(str : String){
nameString = str
print("var : (nameString)")
planningCellHeader?.titleLabel.text = nameString
}
func getLocationMethod(str : String){
locationString = str
print("var : (String(describing: locationString))")
planningCellHeader?.locationTextView.text = locationString
}
func getProfileImageMethod(str : String){
profileImageUrl = str
print("var : (String(describing: profileImageUrl))")
planningCellHeader?.userProfileImageView.imageUrlString = profileImageUrl
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch indexPath.section {
case 0:
switch indexPath.item {
case 0:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "headerId", for: indexPath) as! PlanningCellHeader                
cell.myMethod(str: nameString)
cell.getLocationMethod(str: locationString)
cell.getProfileImageMethod(str: profileImageUrl)
return cell
case 1:
// ...
default:
// ...
}
default:
// ...
}
}

保存视图的标头单元格类

var nameString: String?
var locationString: String?
var profileImageUrl: String?
let titleLabel: UILabel = {
let label = UILabel()
// ...
return label
}()
let locationTextView: UITextView = {
let textView = UITextView()
// ...
return textView
}()
let userProfileImageView: CustomImageView = {
let imageView = CustomImageView()
// ...
return imageView
}()
override init(frame: CGRect) {
super.init(frame: frame)
titleLabel.text = nameString
locationTextView.text = locationString
userProfileImageView.imageUrlString = profileImageUrl
setupViews()
}
func myMethod(str : String){
nameString = str
print("var : (String(describing: nameString))")
titleLabel.text = nameString
}
func getLocationMethod(str : String){
locationString = str
print("var : (String(describing: locationString))")
locationTextView.text = locationString
}
func getProfileImageMethod(str : String){
profileImageUrl = str
print("var : (String(describing: profileImageUrl))")
//        userProfileImageView.image = UIImage(named: "meAndDuncan")
userProfileImageView.imageUrlString = profileImageUrl
}

我自己想通了。也许是不言自明的,但我必须在我的getProfileImageMethod(str:String(中将imageUrlString转换回Data(contentOf:url!(,如下所示:

func getProfileImageMethod(str : String){
profileImageUrl = str
print("var : (String(describing: profileImageUrl))")
// added the code below and it worked!
var imageUrlString: String?
imageUrlString = profileImageUrl
let url = URL(string: imageUrlString!)
let data = try? Data(contentsOf: url!)
let image: UIImage = UIImage(data: data!)!
userProfileImageView.image = image
}

最新更新