2017-03-01 12 views
2

私はuitableviewを持っています。このuitableviewではセルがあります。このセルは、幅が等しい2 uiimageviewをお互いに持っています。この2つのイメージビューでは、幅全体を取ることができます。私はキングフィッシャーから画像を取得していますが、URLから取得した画像はどのように幅に基づいてサイズを変更できますか?たとえば画像が200〜200の場合、半分の幅は400です。画像の高さを400に変更したい場合は、300〜400の場合は150となります。画像のサイズを変更したいまた、処理中のセルのサイズを変更します。キングフィッシャーイメージのサイズを変更してURLからロードしました

これはどのようにキングフィッシャーライブラリーを使用して行うことができますか?

答えて

0

キングフィッシャーを使用して、以下のことを行いました。

let url = URL(string: urlImage) 

    cell.imageView?.kf.setImage(with: url, 
           placeholder: nil, 
           options: [.transition(ImageTransition.fade(1))], 
           progressBlock: { receivedSize, totalSize in 
            print("\(indexPath.row + 1): \(receivedSize)/\(totalSize)") 
    }, 
           completionHandler: { image, error, cacheType, imageURL in 

            print("\(indexPath.row + 1): Finished") 
            print(image?.size) 
            cell.imageView?.image = self.resizeImage(image: image!, newWidth: 40.0) 

    }) 

イメージサイズを変更するためにresizeImage関数を変更すると、正方形のイメージが作成されます。

func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage { 

    let scale = newWidth/image.size.width 
    let newHeight = image.size.height * scale 
    UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight)) 
    image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight)) 
    let newImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return newImage! 
} 

たりすることができ、ユーザKingfisherManager

KingfisherManager.shared.retrieveImage(with: url!, options: nil, progressBlock: nil, completionHandler: { image, error, cacheType, imageURL in 

     cell.imageView?.image = ImageUtils.resizeImage(image: image!, newWidth: 40.0) 

    }) 
関連する問題