构建错误,试图在类 ViewController 中使用 UIImage 数组,以显示彩色矩形

问题描述

我在类 ViewController 中声明了 UIImage 的数组 map_sector_UIImage。

但是在 viewDidLoad() 中,当我将 10 个 UIImage's 附加到数组时出现错误,当我 view.addSubview 时出现错误

每个 UIImage 都是一个彩色方块

我收到构建错误(见下文)。

    NOTE:   let App.total_map_sectors: Int = 10
    

class ViewController: UIViewController 
{
            static var map_sector_UIImage: [UIImage] = [] 

override func viewDidLoad()     
{
App.init_class() 
            // Allocate array of sector UIImage:
            for map_sector_index in 0...App.total_map_sectors-1
            {
                ViewController.map_sector_UIImage[ map_sector_index ] += 
 E R R O R   Referencing operator function '+=' on 'FloatingPoint' requires that 'UIImage' conform to 'FloatingPoint'
 {  
                    let theImage = UIImage()
                    theImage.translatesAutoresizingMaskIntoConstraints = false //You need to call this property so the image is added to your view
                    return theImage
                }()
            }
            super.viewDidLoad()

            for map_sector_index in 0...App.total_map_sectors-1
            {
                view.addSubview( ViewController.map_sector_UIImage[ map_sector_index ] )
E R R O R    Referencing operator function '+=' on 'FloatingPoint' requires that 'UIImage' conform to 'FloatingPoint'
            }


           for sector_index in 0...App.total_map_sectors - 1
           {
                ViewController.map_sector_UIImage[ sector_index ] =
                    // swift 4.0
                    UIColor.black.image(CGSize( width: App.sector_width_pix,height: App.sector_height_pix ))
                    // swift 3.0 = UIImage(color: .blackColor(),size: CGSize(width: App.sector_width_pix,height: App.sector_height_pix ))
                // https://stackoverflow.com/questions/26542035/create-uiimage-with-solid-color-in-swift
                    // eg. let redImage200x200 = UIImage(color: .redColor(),size: CGSize(width: 200,height: 200))
           }
}



        
    // Swift 4.0
    // From:
                // https://stackoverflow.com/questions/26542035/create-uiimage-with-solid-color-in-swift
    extension UIColor {
    func image(_ size: CGSize = CGSize(width: 1,height: 1)) -> UIImage {
        return UIGraphicsImageRenderer(size: size).image { rendererContext in
            self.setFill()
            rendererContext.fill(CGRect(origin: .zero,size: size))
        }
    }
 }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)