Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resize sdwebImage swift

Hello I am displaying images on my app using sdwebImage. I have a code here to resize the image

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

    let scale = newWidth / image.size.width
    let newHeight = image.size.height * scale
    UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight))
    image.drawInRect(CGRectMake(0, 0, newWidth, newHeight))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage
}

The problem is the above function accepts the UIImage as parameter and sdwebimage accepts the URL. How can I call the above resize function in sdwebimage. or in short how Can I resize the image that are presenting through sdwebImage here

   cell.profileImageView.sd_setImageWithURL(UIImage().absoluteURL(profileImageUrl as! String), placeholderImage: UIImage.init(named: "default-profile-icon")?.circle!, completed: completionBlock)
like image 413
hellosheikh Avatar asked Jan 06 '23 09:01

hellosheikh


1 Answers

do like

cell.profileImageView.sd_setImageWithURL(
    NSURL(string: profileImageUrl as! String),
    placeholderImage: UIImage.init(named: "default-profile-icon"),
    options: nil,
    progress: nil,
    completed: { (image: UIImage?, error: NSError?, cacheType: SDImageCacheType!, imageURL: NSURL?) in

        guard let image = image else { return }
        print("Image arrived!")
        cell.profileImageView.image = resizeImage(image, newWidth: 200)
    }
)
like image 162
Anbu.Karthik Avatar answered Jan 08 '23 23:01

Anbu.Karthik