Search code examples
iosswifticonsphotofoursquare

Can't get an image from foursquare api, on swift iOS


Im using das quadrat foursquare sdk for swift (QuadratTouch) everything works fine expect getting images they marked in JSON like NSDictionary with "prefix" and "suffix" and UIImage get nil in the end

Parsing JSON and creating a normal url

let parameters = [Parameter.ll:"\(lat),\(long)", Parameter.limit:"35", Parameter.radius:"\(radius)", Parameter.intent:"browse"]
    let currentTask = session.venues.search(parameters) {
        (result) -> Void in
        var places = [Place]()
        if let response = result.response {
            if let venues =  response["venues"] as? NSArray {
                for venue in venues {
                    if let categories = venue["categories"] as? NSArray {
                       let categoryInfo = categories[0] as! NSDictionary
                              if let  location = venue["location"] as? NSDictionary,
                                name = categoryInfo["name"] as? String,
                                address = location["address"] as? String,
                                lat = location["lat"] as? CLLocationDegrees,
                                long = location["lng"] as? CLLocationDegrees,
                                icon = categoryInfo["icon"] as? NSDictionary,
                                prefix = icon["prefix"] as? String,
                                suffix = icon["suffix"] as? String {
                                    let url = prefix + suffix
                                    let pos = CLLocationCoordinate2DMake(lat, long)
                                    places.append(FourSquarePlace(coordinates: pos, title: name, address: address, imageURL:url, photo: nil))
                            }

and image getting method from SessionAuthorizationDelegate

 self.session.downloadImageAtURL(NSURL(string: imageURL)!, completionHandler: { (imageData, error) -> Void in
                    //print(imageData)
                    print(imageURL)
                    if imageData != nil {
                        thisMarker.icon = UIImage(data: imageData!)
                    }
                })

I've tried to paste the ULR from son to browser and got it

AccessDeniedAccess DeniedAF1DF956303F2D6EcRetLqGf3ipb6KOsSJ9YkqiDnmEUkjhEavefWCzjlHQAivKWMLsRMQukWsYiVCYK

i guess maybe it can't add Access Token to image, but its weird cause I'm usind sdk method...


Solution

  • let url = prefix + suffix

    This usage is incorrect. You have to specify the size of image.

    ex.

    let url = prefix + "100x100" + suffix

    See also Foursquare API document

    To assemble a resolvable photo URL, take prefix + size + suffix, e.g. https://irs0.4sqi.net/img/general/300x500/2341723_vt1Kr-SfmRmdge-M7b4KNgX2_PHElyVbYL65pMnxEQw.jpg.

    size can be one of the following, where XX or YY is one of 36, 100, 300, or 500.

    • XXxYY
    • original: the original photo's size
    • capXX: cap the photo with a width or height of XX (whichever is larger). Scales the other, - smaller dimension proportionally
    • widthXX: forces the width to be XX and scales the height proportionally
    • heightYY: forces the height to be YY and scales the width proportionally