我正在爲Udacity組合項目編寫一個應用程序,該項目將從Flickr獲取照片並顯示在collectionView中。當按下按鈕時,它將刷新collectionView中的照片。但是,儘管每次調用後搜索參數都有不同的頁碼,但我無法獲取新的照片集。我的代碼如下:Swift:從Flickr獲取照片總是獲取相同的照片
func getPhotosURLFromFlickr(_ lat: AnyObject, lon: AnyObject, _ completionHandlerForGetPhotosURL: @escaping (_ imageURL: [String]?, _ error: NSError?) -> Void) {
taskForGetPagesFromFlickr(lat, lon: lon) { (parameters, error) in
if let error = error {
completionHandlerForGetPhotosURL(nil, error as NSError)
} else {
if let parameters = parameters {
print(parameters)
self.taskForGetPhotos(parameters, { (imageURLArray, error) in
if let error = error {
completionHandlerForGetPhotosURL(nil, error as NSError)
} else {
if let imageURLArray = imageURLArray {
completionHandlerForGetPhotosURL(imageURLArray, nil)
}
}
})
}
}
}
}
func taskForGetPagesFromFlickr(_ lat: AnyObject, lon: AnyObject, _ completionHandlerForGetPhotosURL: @escaping (_ parameters: [String: AnyObject]?, _ error: NSError?) -> Void) {
let parameters = [
Constants.ParametersKey.Method: Constants.Methods.PhotoSearch as AnyObject,
Constants.ParametersKey.FlickrAPIKey : Constants.APIInfo.APIKey as AnyObject,
Constants.ParametersKey.Format: Constants.ParametersValues.JSON as AnyObject,
Constants.ParametersKey.NoJSONCallback: Constants.ParametersValues.DisableJSONCallback as AnyObject,
Constants.ParametersKey.PerPage: Constants.ParametersValues.Fifteen as AnyObject,
Constants.ParametersKey.Extras: Constants.ParametersValues.MediumURL as AnyObject]
var parametersWithCoord = parameters
parametersWithCoord[Constants.ParametersKey.lat] = lat
parametersWithCoord[Constants.ParametersKey.lon] = lon
let url = flickrURLFromParameters(parametersWithCoord)
let request = NSMutableURLRequest(url: url)
let task = session.dataTask(with: request as URLRequest) { (data, response, error) in
if let error = error {
completionHandlerForGetPhotosURL(nil, error as NSError)
} else {
self.convertDataWithCompletionHandler(data!, completionHandlerForConvertData: { (results, error) in
if let error = error {
completionHandlerForGetPhotosURL(nil, error as NSError)
} else {
// print(results)
guard let photosDict = results?["photos"] as? [String: AnyObject] else {
let userInfo = [NSLocalizedDescriptionKey : "NoPhotosFound"]
completionHandlerForGetPhotosURL(nil, NSError(domain: "NoPhotosFound", code: 1, userInfo: userInfo))
return
}
guard let pages = photosDict[Constants.ParametersKey.Pages] as? Int else {
let userInfo = [NSLocalizedDescriptionKey : "NoPagesFound"]
completionHandlerForGetPhotosURL(nil, NSError(domain: "NoPagesFound", code: 1, userInfo: userInfo))
return
}
let randomPageIndex = Int(arc4random_uniform(UInt32(pages)))
var searchParameters = parametersWithCoord
searchParameters[Constants.ParametersKey.Page] = randomPageIndex as AnyObject?
print("Number of pages: \(pages)")
print("Random page index: \(randomPageIndex)")
completionHandlerForGetPhotosURL(searchParameters, nil)
}
})
}
}
task.resume()
}
func taskForGetPhotos(_ parameters: [String: AnyObject], _ completionHandlerForGetPhotosURL: @escaping (_ imageURL: [String]?, _ error: NSError?) -> Void) {
let url = flickrURLFromParameters(parameters)
let request = NSMutableURLRequest(url: url)
print(url)
let task = session.dataTask(with: request as URLRequest) { (data, response, error) in
if let error = error {
completionHandlerForGetPhotosURL(nil, error as NSError)
} else {
self.convertDataWithCompletionHandler(data!, completionHandlerForConvertData: { (results, error) in
if let error = error {
completionHandlerForGetPhotosURL(nil, error as NSError)
} else {
// print(results)
guard let photosDict = results?["photos"] as? [String: AnyObject] else {
let userInfo = [NSLocalizedDescriptionKey : "NoPhotosFound"]
completionHandlerForGetPhotosURL(nil, NSError(domain: "NoPhotosFound", code: 1, userInfo: userInfo))
return
}
guard let photosArray = photosDict["photo"] as? [[String: AnyObject]] else {
let userInfo = [NSLocalizedDescriptionKey : "NoPhotosArrayFound"]
completionHandlerForGetPhotosURL(nil, NSError(domain: "NoPhotosArray", code: 1, userInfo: userInfo))
return
}
var imageURLArray: [String] = []
if photosArray.count != 0 {
for pics in photosArray {
guard let imageURLString = pics[Constants.ParametersValues.MediumURL] as? String else {
print("NoImageURLString Found")
return
}
imageURLArray.append(imageURLString)
}
}
print("ImageURLArray : \(imageURLArray)")
completionHandlerForGetPhotosURL(imageURLArray, nil)
}
})
}
}
task.resume()
}
當我的刷新按鈕被竊聽,上面的一套網絡代碼的運行,如果我點擊按鈕兩次,我的報表打印是這樣:
擷取新收集... 項目頁數0號碼:5712 隨機頁面索引:490 https://api.flickr.com/services/rest?page=490&method=flickr.photos.search&format=json&lon=103.7940514843148&api_key=APIKEY & per_page = 15 & LAT = 1.411935988414726 &演員= url_m & nojsoncallback = 1 ImageURLArray: [ 「https://farm3.staticflickr.com/2059/32542810620_37312d07c9.jpg」, 「https://farm3.staticflickr.com/2330/32102051403_46e30a5eec.jpg」, 「https://farm3.staticflickr.com/2104/32529883860_17558a0acf.jpg」, 「https://farm3.staticflickr.com/2595/32778162441_db4a98d3cd.jpg」, 「https://farm3.staticflickr.com/2466/32087213853_257910d32d.jpg」, 「https://farm3.staticflickr.com/2029/32891916665_2d2d177e71.jpg」, 「https://farm4.staticflickr.com/3685/32878992915_38baaf513e.jpg」, 「https://farm1.staticflickr.com/631/32062787803_ed58defea5.jpg」, 「 https://farm3.staticflickr.com/2332/32873171215_c807db5364.jpg 「 」https://farm3.staticflickr.com/2788/32825584606_7d2bff507c.jpg「, 」https://farm3.staticflickr.com/2479/32052120503_1317f70f1a.jpg「, 」https://farm3.staticflickr.com/2909/32023922664_d276f52369.jpg「, 」https://farm1.staticflickr.com/376/32023912434_9b89fc3d7b.jpg「, 」 https://farm1.staticflickr.com/455/32018845514_22681384ae.jpg 「 」 https://farm3.staticflickr.com/2833/32734093131_0e8da333f4.jpg「]
提取新集... 項目頁數0號碼:5712 隨機頁面索引:5383 https://api.flickr.com/services/rest?page=5383&method=flickr.photos.search&format=json&lon=103.7940514843148&api_key=APIKEY & per_page = 15 & LAT = 1.411935988414726 &演員= url_m & nojsoncallback = 1 ImageURLArray: [ 「https://farm3.staticflickr.com/2059/32542810620_37312d07c9.jpg」, 「https://farm3.staticflickr.com/2330/32102051403_46e30a5eec.jpg」, 「https://farm3.staticflickr.com/2104/32529883860_17558a0acf.jpg」, 「https://farm3.staticflickr.com/2595/32778162441_db4a98d3cd.jpg」 「https://farm3.staticflickr.com/2466/32087213853_257910d32d.jpg」, 「https://farm3.staticflickr.com/2029/32891916665_2d2d177e71.jpg」, 「https://farm4.staticflickr.com/3685/32878992915_38baaf513e.jpg」, 「https://farm1.staticflickr.com/631/32062787803_ed58defea5.jpg」, 「https://farm3.staticflickr.com/2332/32873171215_c807db5364.jpg」, 「https://farm3.staticflickr.com/2788/32825584606_7d2bff507c.jpg」, 「https://farm3.staticflickr.com/2479/32052120503_1317f70f1a.jpg」, 「https://farm3.staticflickr.com/2909/32023922664_d276f52369.jpg」, 「https://farm1.staticflickr.com/376/32023912434_9b89fc3d7b.jpg」, 「https://farm1.staticflickr.com/455/32018845514_22681384ae.jpg」, 「https://farm3.staticflickr.com/2833/32734093131_0e8da333f4.jpg」]
你會意識到,儘管該搜索頁面是兩個時代的不同,但實際上imageURLArray日是同樣。我似乎無法確定原因。
有些幫助非常感謝,請致謝!