gpt4 book ai didi

swift - 尝试制作图像数组并将图像传递给 UIImageView

转载 作者:行者123 更新时间:2023-11-28 07:09:51 25 4
gpt4 key购买 nike

我正在尝试将我的图像放入 var picture 中,我将其声明为 [UIImage]。但我一直发现错误......?

我是编程新手。

这是我的代码:

import UIKit
import AVFoundation

class ViewController: UIViewController {



@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var imageView: UIImageView!
@IBAction func playButton(sender: AnyObject) {

}

@IBAction func stopButton(sender: AnyObject) {
player.stop()

}
var player:AVAudioPlayer = AVAudioPlayer()

var files = ["song1", "song2","song3","song4","song5","song6","song7"]

var name:String = ""

var picture:[UIImage] = [
UIImage(named: "pic/1.jpg")!,
UIImage(named: "pic/2.jpg")!,
UIImage(named: "pic/3.jpg")!,
UIImage(named: "pic/4.jpg")!,
UIImage(named: "pic/5.jpg")!,
UIImage(named: "pic/6.jpg")!,
UIImage(named: "pic/7.jpg")!,
UIImage(named: "pic/8.jpg")!, ]


var condition:Bool = false

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.



}

override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent!) {

if event.subtype == UIEventSubtype.MotionShake {





var randomNumber = Int(arc4random_uniform(UInt32(files.count)))


var fileLocation = NSString(string:NSBundle.mainBundle().pathForResource("sounds/" + files[randomNumber], ofType: "mp3")!)


var error:NSError? = nil
player = AVAudioPlayer(contentsOfURL: NSURL(string: fileLocation), error: &error)

player.play()

var i = randomNumber

var image = picture[randomNumber]

imageView.image = image

name = files[randomNumber]
nameLabel.text = name

}




}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

最佳答案

你的图像名称有问题..你确定所有图像都包含在你的捆绑文件中......因为你没有图像在你的文件中然后它会像这样返回

我已经测试过了

enter image description here

在此我的文件中有图像 "no-photo-icon.png" 但没有 "pic/3.jpg" 。所以它给出这样的错误..用完整的细节解释你的错误。

关于swift - 尝试制作图像数组并将图像传递给 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28832019/

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