0

時展開一個可選值,我需要動態調整的imagView滾動時在迅速集合視圖...斯威夫特 - cellForItemAtIndexPath調整圖像

我用這個函數獲取事件的滾動:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

       // get a reference to our storyboard cell 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell 
     let indexPathItem = self.items[indexPath.item] 
     if let myImage = cell.myImage{ 
      myImage.image = imageWithImage(UIImage(named: indexPathItem)!, scaledToSize: CGSize(width: sizeImage, height: sizeImage)) 
     } 

     return cell 
    } 

而此功能調整圖像大小:當運行

func imageWithImage(image:UIImage,scaledToSize newSize:CGSize)->UIImage{ 

    UIGraphicsBeginImageContext(newSize) 
    image.drawInRect(CGRect(x: 0,y: 0,width: newSize.width,height: newSize.height)) 
    let newImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return newImage.imageWithRenderingMode(.AlwaysTemplate) 
} 

調試停止這一行:

myImage.image = imageWithImage(UIImage(named: indexPathItem)!, scaledToSize: CGSize(width: sizeImage, height: sizeImage)) 

錯誤

"fatal error: unexpectedly found nil while unwrapping an Optional value"

工作,如果我替換成這樣:

cell.myImage.image = UIImage(named : self.items[indexPath.item]) 

但顯然它不會降低圖像

能幫我請???

SOLUTION: 我需要調整對象 「cell.myImage」 而不是 「cell.myImage.image」,因此解決了:

cell.myImage.frame = CGRect(x:0.0,y:0.0,width:sizeImage,height:sizeImage) 

並設置ImageView的方式進樣擬合細胞。

+0

如何創建'indexPathItem'對象? –

+0

哦,是的......用這一行:'let indexPathItem = self.items [indexPath.item]'它是我的數組,其名稱爲圖像 – Ray13

+0

您真的需要調整圖像大小嗎?看起來你的滾動會受到影響,爲什麼不把圖像適合imageView? –

回答

0

您可以使用像下面這樣更安全的代碼,以便在解包可選值時避免崩潰。

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

     // get a reference to our storyboard cell 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! YourCell 

     cell.myImage?.image = imageWithImage(UIImage(named: self.items[indexPath.item]), scaledToSize: CGSize(width: sizeImage, height: sizeImage)) 

     return cell 
    } 

    func imageWithImage(image:UIImage?,scaledToSize newSize:CGSize)->UIImage?{ 

     guard let drawImage = image else { 

      return nil 
     } 

     UIGraphicsBeginImageContext(newSize) 
     drawImage.drawInRect(CGRect(x: 0,y: 0,width: newSize.width,height: newSize.height)) 
     let newImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return newImage?.imageWithRenderingMode(.AlwaysTemplate) 
    } 

cellForItemAtIndexPath代碼是不完整的下面一行

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! 

所以我加入YourCell例如。

+0

是的,我現在糾正我的代碼... YourCell是MyCollectionViewCell – Ray13

+0

我嘗試你的代碼,但與「返回newImage?.imageWithRenderingMode(.AlwaysTemplate)」在函數中我看到我的圖像由藍色着色... 如果我刪除該行編譯但不工作... 圖像仍然很大 - Ray13 14分鐘前 – Ray13

+0

實際上,您可以單獨返回newImage,無需調用imageWithRenderingMode,如果您不希望它作爲模板圖像真的。 – Natarajan