SDWebImage Set Image To UIButton in CellView Using

2020-04-30 05:18发布

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 }

1条回答
仙女界的扛把子
2楼-- · 2020-04-30 05:45

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

}
查看更多
登录 后发表回答