Created
March 23, 2021 21:34
-
-
Save bainfu/a053c7c30fcb9e65f627409da901681e to your computer and use it in GitHub Desktop.
The Realm Serverless Function Call to upload a UIImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
import RealmSwift | |
/** | |
* @param image - A UIImage probably taken from a UIImagePickerController | |
* @param fileName - The File Name you might want to have it stored in repo | |
* @param appUser - The currently logged in Realm User | |
*/ | |
func functionImageUpload(image: UIImage, fileName: String = "test_file_upload", appUser: User) { | |
guard let imageDataString = image.dataURI() else { | |
print("Could not convert image jpeg data for: \(fileName)") | |
return | |
} | |
print("Uploading File around: \(imageDataString.count)") | |
// Going to now change imageDataString to a chunked array 5000 characters at a time. | |
let chunkedArray = imageDataString.chunked(into: 5000) | |
let chunkedBSONArray = chunkedArray.compactMap { (chunkedString) -> AnyBSON in | |
return AnyBSON.string(chunkedString) | |
} | |
let bsonParameters = [ | |
AnyBSON.array(chunkedBSONArray), | |
AnyBSON.string(fileName), | |
AnyBSON.string("RealmPOC") | |
] | |
appUser.functions.uploadExample(bsonParameters) { response, error in | |
guard error == nil else { | |
print("Error during uploadExample: \(error!.localizedDescription)") | |
return | |
} | |
guard case let .string(value) = response else { | |
print("Unexpected non-string result: \(response ?? "empty response")") | |
return | |
} | |
print("Finished Uploading image got: \(value)") | |
} | |
} | |
/* Thanks Internet for below */ | |
extension UIImage { | |
func hasAlpha() -> Bool { | |
let noAlphaCases: [CGImageAlphaInfo] = [.none, .noneSkipLast, .noneSkipFirst] | |
if let alphaInfo = cgImage?.alphaInfo { | |
return !noAlphaCases.contains(alphaInfo) | |
} else { | |
return false | |
} | |
} | |
func dataURI() -> String? { | |
var mimeType: String = "" | |
var imageData: Data | |
if hasAlpha(), let png = pngData() { | |
imageData = png | |
mimeType = "image/png" | |
} else if let jpg = jpegData(compressionQuality: 0.1) { | |
imageData = jpg | |
mimeType = "image/jpeg" | |
} else { | |
return nil | |
} | |
let dataUri = "data:\(mimeType);base64,\(imageData.base64EncodedString())" | |
return dataUri | |
} | |
} | |
extension String { | |
public func chunked(into size: Int) -> [String] { | |
var chunks: [SubSequence] = [] | |
var i = startIndex | |
while let nextIndex = index(i, offsetBy: size, limitedBy: endIndex) { | |
chunks.append(self[i ..< nextIndex]) | |
i = nextIndex | |
} | |
let finalChunk = self[i ..< endIndex] | |
if finalChunk.isEmpty == false { | |
chunks.append(finalChunk) | |
} | |
/* Since we want to deal with strings let's jsut map it here... */ | |
return chunks.map { String($0) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment