WIP: fix resizeImageToStrSizeSync

This commit is contained in:
IC Rainbow
2024-10-22 13:04:00 +03:00
parent 87c13d5600
commit e76e82634d
2 changed files with 20 additions and 7 deletions
+19 -6
View File
@@ -103,12 +103,25 @@ public func resizeImageToDataSize(_ image: UIImage, maxDataSize: Int64, hasAlpha
public func resizeImageToStrSizeSync(_ image: UIImage, maxDataSize: Int64) -> String? {
// XXX: only needed when the original encoding isn't available
let tmpFile = generateNewFileName(getTempFilesDirectory().path + "/" + "resize", "png", fullPath: true)
saveFile(image.pngData(), tmpFile) // encode as png and let the backend deal with alpha and formats
let str = chat_resize_image_to_str_size(filePath, maxDataSize)
removeFile(tmpFile)
guard let dataSize = str?.count > 0 else { return nil }
logger.debug("resizeImageToStrSize final \(dataSize)")
return str
// encode as png and let the backend deal with alpha and formats
guard let d = image.pngData() else { return nil }
if let _ = saveFile(d, tmpFile, encrypted: false) {
defer { removeFile(tmpFile) }
let ptr = chat_resize_image_to_str_size(tmpFile, Int32(maxDataSize))
if let ptr = ptr {
let str = fromCString(ptr)
let dataSize = str.count
if dataSize <= 0 { return nil }
logger.debug("resizeImageToStrSize final \(dataSize)")
return str
} else {
logger.error("resizeImageToStrSize failed")
return nil
}
} else {
logger.error("saveFile failed")
return nil
}
}
public func resizeImageToStrSize(_ image: UIImage, maxDataSize: Int64) async -> String? {
+1 -1
View File
@@ -29,7 +29,7 @@ extern char *chat_valid_name(char *name);
extern int chat_json_length(char *str);
extern char *chat_encrypt_media(chat_ctrl ctl, char *key, char *frame, int len);
extern char *chat_decrypt_media(char *key, char *frame, int len);
extern char *chat_resize_image_to_str_size(char *path, int maxSize);
extern char *chat_resize_image_to_str_size(const char *path, int maxSize);
// chat_write_file returns null-terminated string with JSON of WriteFileResult
extern char *chat_write_file(chat_ctrl ctl, char *path, char *data, int len);