2017-03-15 6 views
0

RoundFrameでcollectionViewを設定しようとしていますが、KingFisher(画像キャッシュライブラリ)を使用して取得しています。UICollectionViewCell - Round Downloaded Image

ラウンドコーナー、imageView、またはセル自体で何を設定すべきかわかりません。セルが丸くても、画像は正方形を埋めるように見えます。

これまでのところ、私はこのコードを使用しています(私は画像その平方変更した後、それを設定していけない場合):

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
... 
if let path = user.profilePictureURL { 
        if let url = URL(string: path) { 
         cell.profilePictureImageView.kf.setImage(with: url) 
         cell.profilePictureImageView.layer.cornerRadius = cell.profilePictureImageView.frame.width/2.0 
        } 
       }  

とImageViewの:

class ProfilePicture : UIImageView { 

    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder)! 
     self.commonInit() 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     self.commonInit() 
    } 

    func commonInit(){ 
     self.layer.masksToBounds = false 
     self.layer.cornerRadius = self.layer.frame.width/2.0 
     self.clipsToBounds = true 
    } 
} 

しかし、それはダウンロード初めてイメージ、彼らはこのようなもの(セルの背景が青色である)

Horizontal CollectionView

答えて

0

あなたはこのように使用することができます:あなたが代わりに幅の(縦の)高さを調整することにより、円を得ることができます

imageView.setRounded() 
+0

Ιイメージキャッシングライブラリ(Kingfisher)のコールバックでもこれを呼び出しますが、私はまだ同じエラーです。 (すべての細胞ではなく、ランダムに見える) – PoolHallJunkie

0

:以下のようにメソッドを呼び出して

extension UIImageView { 

    func setRounded() { 
     let radius = CGRectGetWidth(self.frame)/2 
     self.layer.cornerRadius = radius 
     self.layer.masksToBounds = true 
    } 
} 

self.layer.cornerRadius = self.layer.frame.height/2.0 

結果に不満がある場合は、contentMode.scaleAspectを調整する必要があります。長いエッジを調整することで円を得ることができますが、画像全体は表示されません。