gpt4 book ai didi

ios - 类型 'UIImage' 没有成员 'radialGradientImage'

转载 作者:行者123 更新时间:2023-11-30 11:00:12 24 4
gpt4 key购买 nike

我正在尝试在我的 Sprite 套件游戏中的场景背景中使用颜色渐变。所以它看起来类似于:

enter image description here

我需要帮助解决以下错误:

Type 'UIImage' has no member 'radialGradientImage'

代码:

import UIKit
import SpriteKit
import StoreKit
import GameKit
import AVFoundation
import SpriteKit.SKTexture

class Start: SKScene {
let color1 = UIColor(red: 255/255, green: 153/255, blue: 102/255, alpha: 1)
let color2 = UIColor(red: 255/255, green: 204/255, blue: 153/255, alpha: 1)

//Error ->
let backgroundImg = UIImage.radialGradientImage(size: frame.size, outerColor: color1, innerColor: color2)

//

let backgroundTexture = SKTexture(image: backgroundImg)
let background = SKSpriteNode(texture: backgroundTexture)

override func didMove(to view: SKView) {
//Background
background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
addChild(background)
}

static func radialGradientImage(size: CGSize, outerColor: UIColor, innerColor: UIColor) -> UIImage
{
let colorSpace = CGColorSpaceCreateDeviceRGB()
let gradient = CGGradient(colorsSpace: colorSpace, colors: [outerColor.cgColor, innerColor.cgColor] as CFArray, locations: [1.0, 0.0])
let center = CGPoint(x: size.width / 2.0, y: size.height / 2.0)

UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
let imageContext = UIGraphicsGetCurrentContext()

imageContext!.drawRadialGradient(gradient!, startCenter: center, startRadius: 0.0, endCenter: center, endRadius: size.width / 2.0, options: CGGradientDrawingOptions.drawsAfterEndLocation)

let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return image!
}
}

最佳答案

radialGradientImageStart 类的静态方法,而不是 UIImage 类。

你想要:

let backgroundImg = Start.radialGradientImage(size: frame.size, outerColor: color1, innerColor: color2)

但是这将会失败并出现更多错误,因为您无法在那里访问 framecolor1color2

您需要将 let 更改为 lazy var:

lazy var backgroundImg = Start.radialGradientImage(size: frame.size, outerColor: color1, innerColor: color2)

关于ios - 类型 'UIImage' 没有成员 'radialGradientImage',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53468907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com