Brett Taylor Brett Taylor - 1 year ago 198
iOS Question

Swift 3, SDwebimage - How do you set a button placeholder image on image upload with SDWebImage?

I'm creating the ability for users to upload an image using buttons with the image as the background. What I'm trying to do is set "imgTemp" which was the image the user chose from their phone and set it to the background of the button using sdwebimage. How would you do this with no errors? Im getting "Cannot convert value of type 'UIImage?' to expected argument type 'URL?'" which I understand just don't have a clue how to fix it. Thanks.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]){

let imgTemp = info[UIImagePickerControllerOriginalImage] as? UIImage

cell.image01.sd_setBackgroundImage(with: imgTemp, for: UIControlState.normal, placeholderImage: defaultImg)

}

Answer Source

Since the value contained within the UIImagePickerControllerOriginalImage key is a UIImage object you can directly set it as a background image. You do not need to use a method provided by SDWebImage for the same.

cell.image01.setBackgroundImage(imgTemp, for: .normal)

SDWebImage is used for asynchronously loading an image from a given URL.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download