Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UIImageView中更改图像,其他XCode问题_Ios_Xcode_Swift_Uiimageview - Fatal编程技术网

Ios 在UIImageView中更改图像,其他XCode问题

Ios 在UIImageView中更改图像,其他XCode问题,ios,xcode,swift,uiimageview,Ios,Xcode,Swift,Uiimageview,我对XCode(swift)非常陌生,我试图让UIImageView随机显示三个图像中的一个,但是当我切换到模拟器中的第二个viewController时,什么都没有发生,XCode突出显示ViewDidLoad函数的结尾,说Thread 1:breakpoint 1.1 如果有更好或更可靠的方法让UIImageView切换图像,我肯定想知道 我当前的代码: import UIKit let imageInitialIdentity = arc4random_uniform(3) class

我对XCode(swift)非常陌生,我试图让UIImageView随机显示三个图像中的一个,但是当我切换到模拟器中的第二个viewController时,什么都没有发生,XCode突出显示ViewDidLoad函数的结尾,说Thread 1:breakpoint 1.1

如果有更好或更可靠的方法让UIImageView切换图像,我肯定想知道

我当前的代码:

import UIKit

let imageInitialIdentity = arc4random_uniform(3)

class SecondViewController: UIViewController {



    override func viewDidLoad() {
        super.viewDidLoad()



        if(imageInitialIdentity == 0){
            imageGuess.image = UIImage(named: "0ps.pngs")
            imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
        }
        if(imageInitialIdentity == 1){
            imageGuess.image = UIImage(named: "250ps.png")
            imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
        }
        if(imageInitialIdentity == 2){
            imageGuess.image = UIImage(named: "500ps.png")
            imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
        }
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    }


    @IBOutlet weak var imageGuess: UIImageView!

    var imageIdentity = imageInitialIdentity
    var score = 0
    let imageZero = UIImage.init(named: "0ps")
    let image250 = UIImage.init(named: "250ps")
    let image500 = UIImage.init(named: "500ps")





    @IBAction func guessZero(sender: UIButton) {

        //if zero matches the picture 0, add a point
        if imageIdentity == 0{
            score++
        }
        else{
            //print incorrect, corect answer is 0
        }

            //randomizes another picture
            let rand1 = arc4random_uniform(3)
            if rand1 == 0 {
                imageGuess.image = UIImage(named: "0ps.png")
                imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
                imageIdentity = 0
            }
            else if rand1 == 1{
                imageGuess.image = UIImage(named:"250ps.png")
                imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
                imageIdentity = 1
            }
            else if rand1 == 2{
                imageGuess.image = UIImage(named:"500ps.png")
                imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
                imageIdentity = 2
            }

    }

    @IBAction func guess250(sender: UIButton) {
        // if 150 matches the picture of 250, return true
        //randomizes another picture
        //adds one to score?
        if imageIdentity == 1{
            score++
        }


        let rand2 = arc4random_uniform(3)
        if rand2 == 0 {
            imageGuess.image = UIImage(named:"0ps.png")
            imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
            imageIdentity = 0
        }
        else if rand2 == 1{
            imageGuess.image = UIImage(named:"250ps.png")
            imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
            imageIdentity = 1

        }
        else if rand2 == 2{
            imageGuess.image = UIImage(named:"500ps.png")
            imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
            imageIdentity = 2
        }
    }

    @IBAction func guess500(sender: UIButton) {
        //if 500 matches the picture of 500, return true
        //randomizes another picture
        if imageIdentity == 2{
            score++
        }


        let rand3 = arc4random_uniform(3)
        if rand3 == 0 {
            imageGuess.image = UIImage(named:"0ps.png")
            imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
            imageIdentity = 0
        }
        else if rand3 == 1{
            imageGuess.image = UIImage(named:"250ps.png")
            imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
            imageIdentity = 1

        }
        else if rand3 == 2{
            imageGuess.image = UIImage(named:"500ps.png")
            imageGuess.frame = CGRect(x: 51, y: 47, width: 500, height: 340)
            imageIdentity = 2
        }
    }

}

看起来你已经设置了一个断点。通常是在单击查看错误时意外导致的。如果单击类似蓝色箭头的按钮,将停用断点:


如果代码从一个ViewController运行到第二个ViewController,并且程序在viewDidLoad时中断,您可能需要检查代码如何完成其上一个类,以及是什么使其运行第二个ViewController类。检查完之后,您可能希望将随机方法移出viewDidLoad,以找出导致崩溃的确切原因。比如:

override func viewDidLoad() 
{
    super.viewDidLoad()
    randomMethod()
}
func randomMethod() // put a break point here if viewDidLoad passes
{
     if(imageInitialIdentity == 0){
     ..... your code ....
    }
}
回答您的问题,“如果有更好或更可靠的方法让UIImageView切换图像”

简短的回答,不,就是这样做的。 需要注意的一点是,必须在主线程上设置图像才能看到更改。viewDidLoad方法肯定是在主线程上运行的,所以这里不必担心。 如果您决定稍后更新imageview的图像,则可以使用来正确更新UI

dispatch_async(dispatch_get_main_queue(), ^{
    // update your UI here
});
如果需要在imageview中的两个图像之间快速切换,可以使用HighlighteImage属性。 设置此属性的方法与设置图像属性的方法相同(当然是使用不同的图像)。 然后,只需将高亮显示的属性设置为“是”或“否”

UIImageView *iv = [UIImageView new];
UIImage *firstImage = [UIImage imageNamed:@"first_image.png"];
iv.image = firstImage;
iv.highlightedImage = [UIImage imageNamed:@"second_image.png"];
// Call below code whenever you want to switch the image, by setting the boolean.
dispatch_async(dispatch_get_main_queue(), ^{
    iv.highlighted = YES;
});
我建议对imageview的框架使用image size属性

iv.frame = CGRectMake(0, 0, firstImage.size.width, firstImage.size.height);

听起来你好像设置了断点。禁用或删除断点。您可以按照此处的说明关闭断点。