Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Swift 3,4,5...) Download Photo to a FileAssuming we have the ID of a Photo, this example demonstrates how to download the photo image data to a file.
func chilkatTest() { // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // This example assumes a previously obtained an access token let oauth2 = CkoOAuth2()! oauth2.accessToken = "FACEBOOK-ACCESS-TOKEN" let rest = CkoRest()! // Connect to Facebook... var success: Bool = rest.connect("graph.facebook.com", port: 443, tls: true, autoReconnect: true) if success != true { print("\(rest.lastErrorText!)") return } // Provide the authentication credentials (i.e. the access key) rest.setAuthOAuth2(oauth2) // Assumes we've already obtained a Photo ID. var photoId: String? = "10210199026347451" let sbPath = CkoStringBuilder()! sbPath.append("/v2.7/") sbPath.append(photoId) // First we're going to get the photo informaton so we can get the URL of the image file data. // Select the fields we want. // See https://developers.facebook.com/docs/graph-api/reference/photo/ rest.addQueryParam("fields", value: "id,album,images") var responseJson: String? = rest.fullRequestNoBody("GET", uriPath: sbPath.getAsString()) if rest.lastMethodSuccess != true { print("\(rest.lastErrorText!)") return } let json = CkoJsonObject()! json.emitCompact = false json.load(responseJson) // Show the JSON in human-readable format. print("\(json.emit()!)") // Get the image URL. var imageUrl: String? = json.string(of: "images[0].source") print("Downloading from \(imageUrl!)") let sbImageUrl = CkoStringBuilder()! sbImageUrl.append(imageUrl) // Build the output local file path. let sbToPath = CkoStringBuilder()! sbToPath.append("qa_output/fb") sbToPath.append(json.string(of: "id")) var bCaseSensitive: Bool = false if sbImageUrl.contains(".jpg", caseSensitive: bCaseSensitive) == true { sbToPath.append(".jpg") } else { sbToPath.append(".png") } print("Downloading to \(sbToPath.getAsString()!)") // Download using Chilkat HTTP. let http = CkoHttp()! success = http.download(imageUrl, saveToPath: sbToPath.getAsString()) if success != true { print("\(http.lastErrorText!)") } else { print("Downloaded.") } } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.