0

のサイズを変更する際のオプション値をアンラップ...スウィフト - cellForItemAtIndexPath私は迅速でコレクションビューをスクロールするときに動的にimagViewのサイズを変更する必要がある画像

私は、スクロールのGETイベントのために、この機能を使用します。

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

デバッグ停止この行を実行するとエラー

作業

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

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

私はこれであることを置き換えた場合:

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

しかし、明らかにそれは画像

を低下させないplsは私を助けることができます?

SOLUTION: 私はそうでは解決 "cell.myImage.image" オブジェクト "cell.myImageを" サイズを変更していない必要があります。

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

とアスペクトフィットのような細胞へのImageViewのモードを設定します。

+0

'indexPathItem'オブジェクトの作成方法は? –

+0

ありがとうございます。この行で 'let indexPathItem = self.items [indexPath.item]' itemsイメージの名前の配列です – 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コードは、私は例えばYourCellを追加したライン

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

以下不完全です。

+0

はい、私のコードを修正しています... YourCellはMyCollectionViewCellです – Ray13

+0

私はあなたのコードを試していますが、return newImage?.imageWithRenderingMode(.AlwaysTemplate)関数では、私の画像は青色で表示されています... その行を削除してもコンパイルできない場合... imagesまだ大きいです - Ray13 14分前 – Ray13

+0

実際には、あなたは本当にテンプレートイメージとしてそれをしたくない場合は、newImageだけを返すことができ、imageWithRenderingModeを呼び出す必要はありません。 – Natarajan

関連する問題