我试图将我的图片放入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.
}
}
答案 0 :(得分:1)
您的图片名称存在问题..您确定所有图片都包含在您的捆绑文件中吗...因为您没有在您的文件中成像然后它会像这样返回
我测试了这个
在此我的文件中有" no-photo-icon.png" ,但没有" pic / 3。 jpg" 。所以它给出这样的错误.. 用完整的细节解释你的错误。