のサイズを変更する際のオプション値をアンラップ...スウィフト - 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のモードを設定します。
'indexPathItem'オブジェクトの作成方法は? –
ありがとうございます。この行で 'let indexPathItem = self.items [indexPath.item]' itemsイメージの名前の配列です – Ray13
本当にイメージのサイズを変更する必要がありますか?あなたのスクロールが苦しんでいるようですが、イメージをimageViewに合わせないのはなぜですか? –