/* 1 - Define constants */
let BASE_URL = "https://api.flickr.com/services/rest/"
let METHOD_NAME = "flickr.photos.search"
let API_KEY = "2103f3f3de8e1cf11316312322276126"
let EXTRAS = "url_m"
let DATA_FORMAT = "json"
let SAFE_SEARCH="1"
let NO_JSON_CALLBACK = "1"

 

class ViewController: UIViewController {






@IBOutlet weak var mainImg: UIImageView!



@IBOutlet weak var imageTitle: UILabel!



@IBOutlet weak var searchText: UITextField!




@IBAction func searchButton(sender: AnyObject) {

let methodArguments = [
"method": METHOD_NAME,
"api_key": API_KEY,
"text": searchText.text,
"safe_search": SAFE_SEARCH,
"extras": EXTRAS,
"format": DATA_FORMAT,
"nojsoncallback": NO_JSON_CALLBACK
]

getImageFromFlickrSearch(methodArguments)

}

override func viewDidLoad() {
super.viewDidLoad()
// 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.
}

func getImageFromFlickrSearch(methodArguments:[String : AnyObject]) {
/* 3 - Initialize session and url */
let session = NSURLSession.sharedSession()
let urlString = BASE_URL + escapedParameters(methodArguments)
let url = NSURL(string: urlString)!
let request = NSURLRequest(URL: url)

/* 4 - Initialize task for getting data */
let task = session.dataTaskWithRequest(request) {data, response, downloadError in
if let error = downloadError {
println("Could not complete the request \(error)")
} else {
/* 5 - Success! Parse the data */
var parsingError: NSError? = nil
let parsedResult: AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &parsingError)

if let photosDictionary = parsedResult.valueForKey("photos") as? NSDictionary {
if let photoArray = photosDictionary.valueForKey("photo") as? [[String: AnyObject]] {

/* 6 - Grab a single, random image */
let randomPhotoIndex = Int(arc4random_uniform(UInt32(photoArray.count)))



let photoDictionary = photoArray[randomPhotoIndex] as [String: AnyObject]

/* 7 - Get the image url and title */
let photoTitle = photoDictionary["title"] as? String
let imageUrlString = photoDictionary["url_m"] as? String
let imageURL = NSURL(string: imageUrlString!)

/* 8 - If an image exists at the url, set the image and title */
if let imageData = NSData(contentsOfURL: imageURL!) {
dispatch_async(dispatch_get_main_queue(), {
self.mainImg.image = UIImage(data: imageData)
self.imageTitle.text = photoTitle
})
} else {
println("Image does not exist at \(imageURL)")
}
} else {
println("Cant find key 'photo' in \(photosDictionary)")
}
} else {
println("Cant find key 'photos' in \(parsedResult)")
}
}
}

/* 9 - Resume (execute) the task */
task.resume()
}

/* Helper function: Given a dictionary of parameters, convert to a string for a url */
func escapedParameters(parameters: [String : AnyObject]) -> String {

var urlVars = [String]()

for (key, value) in parameters {

/* Make sure that it is a string value */
let stringValue = "\(value)"

/* Escape it */
let escapedValue = stringValue.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())

/* Append it */
urlVars += [key + "=" + "\(escapedValue!)"]

}

return (!urlVars.isEmpty ? "?" : "") + join("&", urlVars)
}

 

 

}//end of class