Skip to content

Instantly share code, notes, and snippets.

@blazovics
Last active September 30, 2021 11:14
Show Gist options
  • Save blazovics/60bd509a286e6acd218c11d3f53b9fd6 to your computer and use it in GitHub Desktop.
Save blazovics/60bd509a286e6acd218c11d3f53b9fd6 to your computer and use it in GitHub Desktop.
func getRandomPicture(_ picture: inout UIImage?, titles: inout [String], pictureTitleIndex: inout Int) {
guard let pictures = pictures else {
return
}
// kiválasztott kép indexe a `pictures` tömbben
let selectedPictureIndex = Int.random(in: 0...pictures.count-1)
//Int(arc4random_uniform(UInt32(pictures.count - 1)))
// tömb a még ki nem választott képcímek indexével
var rangeArray = [Int]()
for index in 0..<pictures.count {
rangeArray.append(index)
}
rangeArray.remove(at: selectedPictureIndex)
print("range array: \(rangeArray)")
// egyedi véletlen képcímek kiválasztása
var titlesToReturn = [String]()
for _ in 0..<choices - 1 {
let randomIndex = Int(arc4random_uniform(UInt32(rangeArray.count - 1)))
if let randomPicture = pictures[rangeArray[randomIndex]] as? [String : String] {
titlesToReturn.append(randomPicture["title"]!)
}
rangeArray.remove(at: randomIndex)
}
print("titles: \(titlesToReturn)")
// választott képcím beszúrása a véletlen képcímek közé, véletlen helyre :)
pictureTitleIndex = Int(arc4random_uniform(UInt32(choices)))
if let chosenPicture = pictures[selectedPictureIndex] as? [String: String] {
titlesToReturn.insert(chosenPicture["title"]!, at: pictureTitleIndex)
picture = UIImage(named: chosenPicture["image"]!)!
}
titles = titlesToReturn
}
override func viewDidLoad() {
super.viewDidLoad()
let pictureManager = (UIApplication.shared.delegate as? AppDelegate)?.pictureManager
var titles = [String]()
pictureManager?.getRandomPicture(&baseImage, titles: &titles, pictureTitleIndex: &correctAnswer)
for index in 1...titles.count {
let button = view.viewWithTag(index) as? UIButton
button?.setTitle(titles[index - 1], for: .normal)
}
if let baseImage = baseImage {
let cropSize = CGSize(width: 300, height: 200)
let cropRect = CGRect(x:CGFloat(Int(arc4random()) % Int(baseImage.size.width - cropSize.width)),
y:CGFloat(Int(arc4random()) % Int(baseImage.size.height - cropSize.height)),
width:cropSize.width * baseImage.scale,
height:cropSize.height * baseImage.scale)
let croppedImageRef = baseImage.cgImage!.cropping(to: cropRect)
let croppedImage = UIImage(cgImage: croppedImageRef!, scale: baseImage.scale, orientation: baseImage.imageOrientation)
pictureView.image = croppedImage
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment