FlickrAPI 麻烦理解

FlickrAPI trouble understanding

我必须制作一个应用程序,让用户在地图上放置一个图钉,然后根据图钉的位置从 Flickr 中提取图片。我的教授给了我们一个 FlickrAPI 文件供我们使用,但我无法理解它在做什么。

import Foundation

class FlickrAPI: NSObject {
static let sharedInstance = FlickrAPI()    // Create a Singleton for the class

var photos: Array<String> = []
var errorLevel = 0

// Flickr Constants
let BASE_URL = "https://api.flickr.com/services/rest/"
let METHOD_NAME = "flickr.photos.search"
let API_KEY = "<API Key Here>"

func fetchPhotosFromFlickrBasedOn(Latitude lat: Double, Longitude lng: Double, PageToFetch pageToFetch: Int, completion: @escaping (_ error: Int, _ pg: Int, _ pgs: Int) -> Void) {
    // Empty our Photos Array
    photos.removeAll(keepingCapacity: true)

    // Build Aurgument List
    let methodArguments = [
        "method": METHOD_NAME,
        "api_key": API_KEY,
        "lat": String(format: "%f", lat),
        "lon": String(format: "%f", lng),
        "accuracy": "15",       // Accuracy (Street Level)
        "radius": "1",          // Distance
        "radius_units": "km",   // in Kilometers,
        "safe_search": "1",     // Safe (G Rated),
        "content_type": "1",    // Photos Only
        "per_page": "100",      // Photos per Page
        "page": "\(pageToFetch)",
        "extras": "url_m",      // Return Photo URLs
        "format": "json",       // Request JSON data format
        "nojsoncallback": "1"   // No JSON Callback
    ]

    // Initialize Shared Session
    let session = URLSession.shared
    let url = URL(string: BASE_URL + escapeUrlParameters(methodArguments))!
    let request = URLRequest(url: url)
    var page = 0
    var pages = 0

    // Setup Session Handler
    let task = session.dataTask(with: request, completionHandler: {data, response, error in
        self.errorLevel = 0  // Initialize Error Level
        if error != nil {
            self.errorLevel = 1  //***** Network Error
        } else {
            // Okay to Parse JSON
            do {
                let parsedResult: AnyObject = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as AnyObject
                if let photosDictionary = parsedResult.value(forKey: "photos") as? NSDictionary {
                    if let photoArray = photosDictionary.value(forKey: "photo") as? [[String: AnyObject]] {
                        page = photosDictionary["page"] as! Int
                        pages = photosDictionary["pages"] as! Int
                        for photoDictionary in photoArray {
                            if let photoUrl = photoDictionary["url_m"] as? NSString {
                                let ext = photoUrl.pathExtension
                                let noExt = photoUrl.deletingPathExtension
                                let addThumbDesignation = (noExt + "_q_d") as NSString
                                let thumbUrl = addThumbDesignation.appendingPathExtension(ext)
                                self.photos.append(thumbUrl!)
                            } else {
                                NSLog("***** Could not obtain an Image URL at Index:%d for Owner:%@", self.photos.count, photoDictionary["owner"] as! String)
                            }
                        }
                    } else {
                        self.errorLevel = 4  //***** No "Photo" Array key present
                    }
                } else {
                    self.errorLevel = 3  //***** No "Photos" Dictionary key present
                }
            } catch {
                self.errorLevel = 2  //***** Parsing Error
            }
            completion(self.errorLevel, page, pages)
        }
    }) 
    task.resume()
}

// Escape URL Parameters
func escapeUrlParameters(_ parms: [String : String]) -> String {
    var urlParms = [String]()
    for (key, value) in parms {
        let escapedValue = value.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
        urlParms += [key + "=" + "\(escapedValue!)"]
    }
    return urlParms.isEmpty ? "" : "?" + urlParms.joined(separator: "&")
}
}

在我的代码中我有这个函数:

@IBAction func addPin(_ gestureRecognizer: UILongPressGestureRecognizer) {
    var touchPoint = gestureRecognizer.location(in: Map)
    var newCoordinates = Map.convert(touchPoint, toCoordinateFrom: Map)
    let annotation = MKPointAnnotation()
    annotation.coordinate = newCoordinates
    Map.addAnnotation(annotation)

    flickr.fetchPhotosFromFlickrBasedOn(Latitude: annotation.coordinate.latitude, Longitude: annotation.coordinate.longitude, PageToFetch: 1, completion: {error,pg,pgs in
print("Error: \(error)")
print("Page: \(pg)")
print("Pages: \(pgs)")
})
    //flickr.escapeUrlParameters(<#T##parms: [String : String]##[String : String]#>)
}

我知道代码应该发送 json 我应该用来在我的应用程序上显示图像结果的信息,但就像我说的那样,我很难理解我教授的代码。我的主要问题是调用该函数后,它会跳过 运行 拉取照片所需的所有内容。我显然缺少我需要的东西,这样我的代码才能执行它需要的东西。

您在 FlickrAPI 中的 radius 参数指定的半径对 Flickr 的 API 无效。它应该低于 50。如果那是你想走的路线,你将不得不尝试看看你可以逃脱的最大值是多少。

我建议使用 Postman 来测试 URL,而不是在 Xcode 中调试 API 问题。在 Xcode 中拨打电话之前,先在群组中获取 API 面向电话的便便。这样就容易多了。在这种情况下,只需在 URL 所在的位置设置断点,在调试器中键入 po url,将 URL 复制到 Postman and see what comes back in Postman. API's like Flickr are pretty good at telling you what ****ed up. Also, don't be afraid to look at documentation for stuff. It makes life a lot easier. The documentation for this endpoint is located here.

当您到达完成调用时,放置另一个断点并键入 po photos。它将打印出一个字符串数组,这些字符串是 URL 的照片。

如果您只是在地图上点击,您可能不需要 info.plist 中的位置管理器或使用说明。最后,不要在 GitHub 或 SO 等开放论坛中丢掉 API 密钥。