生成不重复的随机UIImage

时间:2017-11-02 01:46:54

标签: ios swift xcode uikit

在我的应用程序项目中,我随机显示了107个图像,我已经设置了一个for循环,将所有图像放入一个数组中。然后我接受该数组并选择一个随机索引。该索引与图片相关,然后当用户向左滑动时该图片出现在屏幕上。我的问题是我可以这样做,所以我的代码不会在数组中重复相同的索引,直到所有这些索引(或关闭应用程序之前的数量)都是随机选择的。这是我的代码

class ViewController: UIViewController {

var pictureArray: [String] = []
@IBOutlet weak var quoteImage: UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()

    for i in 0...107{
        pictureArray.append("quote\(i).jpg")
        print(pictureArray)
    }

    // Do any additional setup after loading the view, typically from a nib.
    let swipeRecognizer = UISwipeGestureRecognizer(target: self, action: #selector (changeQuotes))
    swipeRecognizer.direction = UISwipeGestureRecognizerDirection.left
    self.view.addGestureRecognizer(swipeRecognizer)
}

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

}
override var prefersStatusBarHidden: Bool{
    return true
}


@objc func changeQuotes(){

    let numberOfImages: UInt32 = 107
    let randomIndex = Int(arc4random_uniform(UInt32(pictureArray.count)))
    let imageName = "\(pictureArray[randomIndex])"
    print(imageName)
    quoteImage.image = UIImage(named: imageName)


    }
}

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

随机刷新您的数组,然后逐个获取“已排序”的图像。

extension Sequence {
    func randomSorted() -> [Element] {
        var result = Array(self)
        guard result.count > 1 else { return result }

        for (i, k) in zip(result.indices, stride(from: result.count, to: 1, by: -1)) {
            let newIndex = Int(arc4random_uniform(UInt32(k)))
            if newIndex == i { continue }
            result.swapAt(i, newIndex)
        }
        return result
    }
}

答案 1 :(得分:0)

您可以通过管理2个阵列来生成唯一的随机数,其中一个阵列包含尚未显示的图像和一个已经显示的图像

在代码中进行以下更改

var pictureArray: [String] = []
var selectedPictureArray: [String] = []

static let TOTAL_IMAGES = 108

@IBOutlet weak var quoteImage: UIImageView!
override func viewDidLoad() {

    super.viewDidLoad()
    self.fillPictureArray()
    let swipeRecognizer = UISwipeGestureRecognizer(target: self, action: #selector (changeQuotes))
    swipeRecognizer.direction = UISwipeGestureRecognizerDirection.left
    self.view.addGestureRecognizer(swipeRecognizer)
}

func fillPictureArray() {

    self.selectedPictureArray.removeAll()
    for i in 0..<ViewController.TOTAL_IMAGES {
        pictureArray.append("quote\(i).jpg")
    }
    print(pictureArray)
}

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

}
override var prefersStatusBarHidden: Bool{
    return true
}


func changeQuotes(){

    if self.pictureArray.count == 0 {

        self.fillPictureArray()
    }

    var randomIndex = 0
    while true {

        randomIndex = Int(arc4random_uniform(UInt32(ViewController.TOTAL_IMAGES)))
        if self.selectedPictureArray.contains("quote\(randomIndex).jpg") {
            continue
        }else {
            break
        }
    }
    let imageName = "quote\(randomIndex).jpg"
    self.selectedPictureArray.append(imageName)
    print(imageName)
    quoteImage.image = UIImage(named: imageName)
    let index = self.pictureArray.index(of: imageName)
    self.pictureArray.remove(at: index!)
}

快乐编码:)