Ios 更改UIImageView’;s图像不’;行不通
我有一个结构,它包含一个名为userProfileImage的静态变量,这个结构位于名为UserProfilePage.swift的视图控制器中,下面是代码:Ios 更改UIImageView’;s图像不’;行不通,ios,swift,struct,uiimage,Ios,Swift,Struct,Uiimage,我有一个结构,它包含一个名为userProfileImage的静态变量,这个结构位于名为UserProfilePage.swift的视图控制器中,下面是代码: struct UserProfile { static let userProfileImage = UIImageView(image: #imageLiteral(resourceName: "profilePicture")) } class UserProfilePage: UIViewController {
struct UserProfile {
static let userProfileImage = UIImageView(image: #imageLiteral(resourceName: "profilePicture"))
}
class UserProfilePage: UIViewController {
// Some code that sets the userProfileImage to another image
}
以下代码位于另一个swift文件中,该文件具有名为Downloads的结构:
struct Downloads {
guard let profilePicURL = URL(string: profilePictureString) else {
UserProfile.userProfileImage.image = UIImage(named: "profilePicture")
print("Profile picture set to default profile picture")
return
}
// Some code
}
当
profilePicURL
不为空时,会执行某些代码,但当它为空时(等于“”
),会执行else块中的代码。问题是配置文件图片没有改变,它只是在else块内执行print语句。有人知道我的代码有什么问题吗?首先,你必须更改你的用户档案图像。这应该是一个var,而不是**static let
您可以在代码中使用带有异步调用的if-let语句。请尝试以下代码
let profilePictureString = "http://SOME URl STRING.."
if let profilePicURL = URL(string: profilePictureString){
// Plcae Your Network Code Here.
} else {
DispatchQueue.main.async {
UserProfile.userProfileImage.image = UIImage(named: "profilePicture")
print("Profile picture set to default profile picture")
}
}
首先,您必须更改用户档案图像。这应该是一个var,而不是**static let 您可以在代码中使用带有异步调用的if-let语句。请尝试以下代码
let profilePictureString = "http://SOME URl STRING.."
if let profilePicURL = URL(string: profilePictureString){
// Plcae Your Network Code Here.
} else {
DispatchQueue.main.async {
UserProfile.userProfileImage.image = UIImage(named: "profilePicture")
print("Profile picture set to default profile picture")
}
}
使用图像文字
struct Downloads {
guard let profilePicURL = URL(string: profilePictureString) else {
UserProfile.userProfileImage.image = imageLiteral(resourceName: "profilePicture")
print("Profile picture set to default profile picture")
return
}
// Some code
}使用图像文字
struct Downloads {
guard let profilePicURL = URL(string: profilePictureString) else {
UserProfile.userProfileImage.image = imageLiteral(resourceName: "profilePicture")
print("Profile picture set to default profile picture")
return
}
// Some code
}您可以调用
setNeedsDisplay()
来更新imageview
DispatchQueue.main.async {
UserProfile.userProfileImage.image = UIImage(named: "profilePicture")
UserProfile.userProfileImage.setNeedsDisplay()
}
您可以调用
setNeedsDisplay()
来更新imageview
DispatchQueue.main.async {
UserProfile.userProfileImage.image = UIImage(named: "profilePicture")
UserProfile.userProfileImage.setNeedsDisplay()
}
检查
UIImage(名为:“profilePicture”)
是否返回图像对象我在其他视图控制器中执行了此操作,并且它确实返回了一个对象“userProfileImage”是静态的,您如何为其分配新值。@Vikky他正在更改其属性而不是整个对象。是Cerlin所说的@VikkyCheck ifUIImage(名为:“profilePicture”)
正在返回一个图像对象我在其他视图控制器中已经这样做了,并且它确实返回了一个对象“userProfileImage”是静态的,您如何给它赋值。@Vikky他正在更改它的属性,而不是整个对象。是的Cerlin所说的@VikkyHey,谢谢您的回答,但它不起作用:(同样,打印语句得到执行,但图片没有更改。请更改您的userProfileImage。这应该是一个变量,而不是**static Let。我尝试了static var,但不起作用,我无法删除static,因为这样我将无法使用另一个视图控制器访问它。谢谢您的回答,但它不起作用:(同样,打印语句得到执行,但图片没有更改。请更改您的userProfileImage。这应该是一个变量,而不是**static Let。我尝试了static var,不起作用,我无法删除static,因为这样我将无法使用另一个视图控制器访问它。嘿,谢谢您的回答,但只是尝试了,不起作用:(嘿,谢谢你的回答,但只是试了一下,不起作用:(