简体   繁体   English

SWIFT(iOS)中的“无法将图像分配给* UIImageView *”错误

[英]“Cannot assign image to *UIImageView*” Error in SWIFT (iOS)

Im trying to find the UIImageViews in the view and to assign few images to them based on the tags. 我试图在视图中找到UIImageViews并根据标签为它们分配几个图像。 But unfortunately i couldn't assign the images to UIImageView variable from an array. 但不幸的是,我无法将图像分配给数组中的UIImageView变量。 I couldn't really resolve the problem. 我无法真正解决问题。

Below is the code that I wrote. 下面是我写的代码。

var imageViewSlot = UIImageView()
    for imageViewSlot in self.view.subviews
    {
       if (imageViewSlot .isMemberOfClass(UIImageView))
       {
            for i in 1...imagePieces.count
            {
                if (imageViewSlot.tag == i)
                {
                    imageViewSlot.image = imagePieces[i] as? UIImage //Error in this line
                }
            }
        }
    }

Any help will be appreciated. 任何帮助将不胜感激。 Thanks in advance. 提前致谢。

  • in above code var imageViewSlot = UIImageView() you gave this as global, and you used same name in for in statement that is the main problem. 在上面的代码var imageViewSlot = UIImageView()你把它作为全局,你在in语句中使用相同的名称是主要问题。 So because of this it donot know which type of object this is, So for that we have to mention like this (imageViewSlot as UIImageView).image = image 因此,它不知道这是什么类型的对象,所以我们必须提到这样(imageViewSlot as UIImageView).image = image

try below code, and check this link 尝试下面的代码,并检查此链接

 for  imageViewSlot in self.view.subviews
    {
        if (imageViewSlot .isMemberOfClass(UIImageView))
        {
            for i in 1...imagePieces.count
            {
                if (imageViewSlot.tag == i)
                {
                    var image = UIImage(named: imagePieces[i])
                    (imageViewSlot as UIImageView).image = image

                }
            }
        }
    }

First, you're accessing your array with an index outside its bounds. 首先,您正在使用超出其边界的索引访问您的数组。 Your for loop should be from 0 up to and not including imagePieces.count . 你的for循环应该从0到最多, 包括imagePieces.count

for i in 0..<imagePieces.count
{
    if (imageViewSlot.tag == i)
    {
        imageViewSlot.image = imagePieces[i] as? UIImage //Error in this line
    }
}

Second, it looks like you're trying to find a UIImageView in your self.view.subviews , right? 其次,看起来你正试图在你的self.view.subviews找到一个UIImageView ,对吧? When you do that, you're setting imageViewSlot to a UIView . 当你这样做时,你将imageViewSlot设置为UIView I see that you're checking if it's a member of class UIImageView , but that doesn't actually change the type of the imageViewSlot variable. 我看到你正在检查它是否是类UIImageView的成员,但实际上并没有改变imageViewSlot变量的类型。 You'll need to cast imageViewSlot to a UIImageView to be able to set the image property. 您需要将imageViewSlot转换为UIImageView才能设置image属性。

I'd do it like this (untested): 我这样做(未经测试):

// Note, you don't need to declare var imageViewSlot first

for subview in self.view.subviews {
    if let imageViewSlot = subview as? UIImageView {
        for i in 0..<imagePieces.count {
            if imageViewSlot.tag == i {
                imageViewSlot.image = imagePieces[i] as UIImage
            }
        }
    }
}

If your tags are setup correctly, then you really don't need that inner for loop either. 如果你的标签设置正确,那么你真的不需要那个内部for循环。 You can simplify it to something like this: 您可以将其简化为以下内容:

for subview in self.view.subviews {
    if let imageViewSlot = subview as? UIImageView {
        if imageViewSlot.tag >=0 && imageViewSlot < imagePieces.count {
            imageViewSlot.image = imagePieces[imageViewSlot.tag] as UIImage
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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