3

我有很多的麻煩加載CustomImageView:在UIImageView的一個詳細的viewController根據collectionViewCell的selectedIndexPath。我已成功地傳遞並加載了UILabels和UITextViews,但無法使用相同的進程和代碼邏輯從同一個selectedIndexPath加載CustomImageView:UIImageView。我相信它與清除或重置我的圖像緩存有關,但不確定要執行此操作的位置或確切要執行的代碼。對不起,任何多餘的代碼,只是想徹底。感謝您的幫助或指導!負載selectedIndexItem(無效imageCache)

//模型對象類從火力地堡

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存儲值:擴展的UIImageView用於填充第一collectionViewController

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() 
    } 
} 

//規劃小區類在第一的CollectionView

在第一collecti
class BasePlanningCell: BaseCell2 { 

    var currentPlanners = [CurrentPlanner]() 

    var currentPlanner: CurrentPlanner? { 
     didSet { 
      setupProfileImage() 
     } 
    } 

    fileprivate func setupProfileImage() { 
     if let profileImageUrl = currentPlanner?.profileImageUrl {   
userProfileImageView.loadImageUsingCacheWithUrlString(profileImageUrl) 
     } 
    } 

//抽頭細胞類onView - 呼叫委託方法

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

//第一collectionViewController類 - 方法對細胞進行抽頭

func showPlanningViewDetailView(indexPath: IndexPath) { 
     let plannersDetailVC = PlanningPlaceDetailsVC() 

     plannersDetailVC.profileImageUrl = plannedPlaces[indexPath.row].profileImageUrl! 
     print(plannersDetailVC.profileImageUrl!) 

     show(plannersDetailVC, sender: self) 
    } 

//第二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: 
      // ... 
     } 
    } 

//第二DetailViewControllers細胞委託類

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: 
      // ... 
     } 
    } 

// headerCell類持有的意見

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 
    } 
+0

什麼是確切的問題,「我不能夠加載CustomImageView:使用相同的進程和代碼邏輯從相同的selectedIndexPath的UIImageView「? – shallowThought

+0

我已成功地通過並顯示基於在所述第一主collectionViewController選擇的單元被顯示在標題單元格類之間的nameString和locationString值。但經過profileImageUrl字符串不加載所選單元profileImage(這是CustomImageView:UIImageView類(佔位符圖像總是出現) – user3708224

+0

什麼是'profileImageUrl' – shallowThought

回答

0

我想出了我自己的。也許自我解釋,但我不得不在imageUrlString轉換回數據(contentsOf:URL)在我getProfileImageMethod(STR:字符串),如下圖所示:

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 
} 
相關問題