我正在为Udacity投资组合项目编写一个应用程序,该项目将从Flickr获取照片并显示在collectionView中。按下按钮时,它将刷新collectionView中的照片。但是,尽管搜索参数在每次调用后都有不同的页码,但我无法获得新的照片集。我的代码如下:
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实际上是相同的。我似乎无法确定原因。
非常感谢一些帮助,谢谢!
答案 0 :(得分:0)
不知怎的,我设法解决了我的问题。我将randomPageIndex限制为最大数量为100,并成功刷新页面。
也许Flickr有一些我可能不知道的最大搜索页面。