如何用用户选择的照片替换图像

互联网 行业动态 更新时间:2024-06-13 00:19:07

Muh*_*waz 5

您可以使用以下方法为您的 UIButtons 分配标签并在 UIButton 操作上通过管理对象将您的图像添加到数组的特定索引上

例如

var selectedIndex or selectedTag // of your UIButton

在按钮上单击更新您的 selectedIndex 值

self.selectedIndex = sender.tag

然后在您的didFinishPicking方法中使用 selectedIndex 上的选定图像更新您的图像数组,并使用以下方式更新您的按钮图像

images.insert(yourSelectedImage, at: selectedIndex)
self.setImagesForButtons()

现在使用 UIButton Outlet 将您选择的图像分配给 UIButton 例如,您的五个按钮有五个插座

@IBOutlet weak var yourButtonOutlet1: UIButton!
@IBOutlet weak var yourButtonOutlet2: UIButton!
@IBOutlet weak var yourButtonOutlet3: UIButton!
@IBOutlet weak var yourButtonOutlet4: UIButton!
@IBOutlet weak var yourButtonOutlet5: UIButton!



 func setImagesForButtons()
    {
        if (self.selectedIndex == 0)
        {
            if (images[selectedIndex] != nil)
            {
                
                self.yourButtonOutlet1.setImage(images[selectedIndex], for: .normal)
            }
            else
            {
                // set plus icon image to your button
            }
        }
        else if (self.selectedIndex == 1)
        {
            if (images[selectedIndex] != nil)
            {
                
                self.yourButtonOutlet2.setImage(images[selectedIndex], for: .normal)
            }
            else
            {
                // set plus icon image to your button
            }
        }
        else if (self.selectedIndex == 2)
        {
            if (images[selectedIndex] != nil)
            {
                
                self.yourButtonOutlet3.setImage(images[selectedIndex], for: .normal)
            }
            else
            {
                // set plus icon image to your button
            }
        }
        else if (self.selectedIndex == 3)
        {
            if (images[selectedIndex] != nil)
            {
                
                self.yourButtonOutlet4.setImage(images[selectedIndex], for: .normal)
            }
            else
            {
                // set plus icon image to your button
            }
        }
        else if (self.selectedIndex == 4)
        {
            if (images[selectedIndex] != nil)
            {
                
                self.yourButtonOutlet5.setImage(images[selectedIndex], for: .normal)
            }
            else
            {
                // set plus icon image to your button
            }
        }
    }

更多推荐

如何用,图像,照片,用户

本文发布于:2023-04-20 20:55:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/hyzx/9efa37c15f234119880daa79aafd7845.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何用   图像   照片   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!