Swift
Swift
Download Image (JPG, GIF, etc.) to Base64
See more HTTP Examples
Demonstrates how to download an image, or any type of file, to get the data in base64 encoding format.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = CkoHttp()!
http.keepResponseBody = true
let bd = CkoBinData()!
success = http.downloadBd(url: "https://www.chilkatsoft.com/images/starfish.jpg", binData: bd)
var statusCode: Int = http.lastStatus.intValue
if success == false {
if statusCode == 0 {
// Unable to either send the request or get the response.
print("\(http.lastErrorText!)")
}
else {
// We got a response, but the status code was not in the 200s
print("Response status code: \(statusCode)")
// Examine the response body.
print("Response body:")
print("\(http.lastResponseBody!)")
}
print("Download failed.")
}
else {
print("Download success, response status = \(statusCode)")
var base64_image_data: String? = bd.getEncoded(encoding: "base64")
print("image data in base64 format:")
print("\(base64_image_data!)")
}
}