简体   繁体   中英

SDWebImage Set Image To UIButton in CellView Using Swift

Hi I am using SDWebImage to set image to a button in xcode 10.2.1.

I am trying the following

import SDWebImage

cell.bProfileImage.sd_setImage(with:URL(string:
individualChatsListArray[indexPath.row].profile_image), forState:.normal)

But I am getting the following error

Value of optional type 'UIButton?' must be unwrapped to refer to member
 'sd_setImage' of wrapped base type 'UIButton'

In the nib file the items are linked and declared as per below

@IBOutlet weak var bProfileImage: UIButton!


@IBAction func bProfileImage(_ sender: Any) {

    print("touch kia")

}

I have tried

cell.bProfileImage?.sd_setImage(with:URL(string:
individualChatsListArray[indexPath.row].profile_image), forState:.normal)

But then getting the following error

Ambiguous reference to member 'bProfileImage'

As you can see I am new to xcode/swift

The following is not giving any error but is also not loading the image

cell.bProfileImage.sd_setBackgroundImage(with: URL(string:
individualChatsListArray[indexPath.row].profile_image), for:
UIControl.State.normal) { (image, error, cache, url) in }

The following is working perfectly.

cell.bProfileImage.sd_setBackgroundImage(with: URL(string:
individualChatsListArray[indexPath.row].profile_image), for:
UIControl.State.normal, placeholderImage: UIImage(named:
"default_profile"), options: SDWebImageOptions(rawValue: 0)) { (image,
error, cache, url) in

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM