From e76e82634d214b788b7cbfdeaf09132bcc300aa3 Mon Sep 17 00:00:00 2001 From: IC Rainbow Date: Tue, 22 Oct 2024 13:04:00 +0300 Subject: [PATCH] WIP: fix resizeImageToStrSizeSync --- apps/ios/SimpleXChat/ImageUtils.swift | 25 +++++++++++++++++++------ apps/ios/SimpleXChat/SimpleX.h | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/apps/ios/SimpleXChat/ImageUtils.swift b/apps/ios/SimpleXChat/ImageUtils.swift index 9bf15992db..e0bcce2e94 100644 --- a/apps/ios/SimpleXChat/ImageUtils.swift +++ b/apps/ios/SimpleXChat/ImageUtils.swift @@ -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? { diff --git a/apps/ios/SimpleXChat/SimpleX.h b/apps/ios/SimpleXChat/SimpleX.h index cb94477a33..7a492c8c4e 100644 --- a/apps/ios/SimpleXChat/SimpleX.h +++ b/apps/ios/SimpleXChat/SimpleX.h @@ -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);