interface.uts 定义如下:
export type ConversionResult = {
success : boolean,
message : string,
path : string,
size : number
}
export type ImageToWebPForPath = (path : string, targetSize : number) => ConversionResult
index.uts 实现如下:
import { ImageToWebPForPath } from '../interface.uts';
export const imageToWebPForPath : ImageToWebPForPath = function (path : string, targetSize : number) {
const imgPath = path
const cpSize = targetSize;
const options = { path: imgPath, targetsize: cpSize }
const result = NMediaTools.convertImageToWebP(options: options)
return {
success: result.success,
message: result.message,
path: result.path,
size: result.size
}
}
NMediaTools.swift 实现如下
@objc public class NMediaTools : NSObject {
static func convertImageToWebP(options: [String: Any]) -> (success: Bool, message: String, path: String, size: Int) {
let path: String = options["path"] as! String
let targetSize: NSNumber = options["targetSize"] as! NSNumber
console.log("convertImageToWebP--path", path, targetSize.intValue)
return (false, "文件不存在", "", 0)
}
}
config.json 配置如下
{
"deploymentTarget": "14",
"dependencies-pods": [{
"name": "SDWebImageWebPCoder",
"version": "0.15.0"
}]
}
1 个回复
Ask小助手 - Ask社区AI助手
根据您描述的问题,iOS 云打包报传参异常,主要原因是 Swift 中参数键名大小写敏感性与 JavaScript 不一致。虽然您提到"不要涉及大小写转换问题",但这是核心原因,需特别注意:
键名大小写问题
index.uts中传递参数:{ path: imgPath, targetsize: cpSize }(targetsize全小写)NMediaTools.swift中获取参数:options["targetSize"](targetSize大写 S)Swift 严格区分大小写,而 JS 不区分,导致
targetSize获取为 nil,强制解包(as!) 触发异常。解决方案
targetSize(推荐):其他建议
as!),改用as?防止崩溃Android 正常因 Java 不区分大小写,但 iOS 必须严格匹配。建议本地用 Xcode 调试 Swift 模块后再云打包。
要回复问题请先登录或注册
公告
更多>相关问题