Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kingfisher image resize on image loaded from url

Tags:

I HAVE uitableview, and in this uitableview I got a cell. This cell has a 2 uiimageview next to each other with equald widths.I want both this 2imageviews to take the entire width, 1st imageview half the width screen second imageview the other half. I am getting the images from kingfisher, how can i the image got from url be resized based on width. if image is 200 - 200 for example, and half width i got is 400..i want my image height to resize to 400. If I got 300 - 400, I and I got width 150..I want my image height to resize to 200.And also resize the cell in process.

How can this be done using kingfisher library?

like image 247
Zenzen Avatar asked Mar 01 '17 17:03

Zenzen


1 Answers

Using Kingfisher i did the following.

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)

    })

You cand modify the resizeImage function in order to change your image size, in my case i made a square image.

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

or you can user 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)

    })
like image 108
Isaias Carrera Avatar answered Sep 21 '22 10:09

Isaias Carrera