9***@qq.com
9***@qq.com
  • 发布:2026-03-06 16:41
  • 更新:2026-03-06 16:42
  • 阅读:20

我的xcframework内部有一个.h文件用于申明了原生方法,那我的index.uts中应该怎么写才能调用到framework里的原生方法?

分类:uts

ADY_IMG_SDK.framework文件放在了\uni_modules\yolo-uts\utssdk\app-ios\Frameworks\ADY_IMG_SDK.xcframework\ios-arm64这个目录下
module.modulemap文件里申明了sdk的头文件

framework module ADY_IMG_SDK {  
    umbrella header "ADY_IMG_SDK.h"  
    export *  
    module * { export * }  
}

Headers里有两个头文件一个是ADY_IMG_SDK.h

#import <Foundation/Foundation.h>  
#import <ADY_IMG_SDK/yolo_api.h>

一个是yolo_api.h

#pragma once  

#ifdef __cplusplus  
extern "C" {  
#endif  

// 初始化模型(这里只是演示)  
bool yolo_load_model(const char* param_path, const char* bin_path);  

// 简单检测接口,返回检测到的数量,out_points 每个对象 4 点 (x,y)  
int yolo_detect(  
//    const unsigned char* rgb_data,  
    const unsigned char* rgba_data,  
    int img_w,  
    int img_h,  
    int* out_points,  
    int max_objects  
);  

int yolo_rectify_by_points(  
    const unsigned char* img_data,  
    int w,  
    int h,  
    const int* points,  
    unsigned char* out_buffer,  
    int max_buffer_size,  
    int* out_w,  
    int* out_h  
);  
#ifdef __cplusplus  
}  
#endif

我的uts组件应该如何写config.json 和 index.uts才能调用的内部的原生方法

2026-03-06 16:41 负责人:无 分享
已邀请:
Ask小助手

Ask小助手 - Ask社区AI助手

欢迎到专业群( uni-app 官方技术交流群 1 ) 咨询,群中有相关专业的管理员和群友。

在 uni-app 的 UTS 插件中调用 iOS 原生 .xcframework 内声明的 C 接口(如 yolo_api.h 中的函数),需要完成以下两步配置:


✅ 1. 配置 config.json(路径:uni_modules/yolo-uts/utssdk/app-ios/config.json

确保声明了依赖的 framework 和系统库:

{  
  "frameworks": [  
    "ADY_IMG_SDK.xcframework"  
  ],  
  "systemFrameworks": [  
    "Foundation.framework"  
  ]  
}

注意:.xcframework 必须放在 utssdk/app-ios/Frameworks/ 目录下,如你所示路径。


✅ 2. 在 index.uts 中声明并调用原生方法(路径:uni_modules/yolo-uts/utssdk/app-ios/index.uts

由于你提供的接口是 C 函数(非 OC/Swift 类方法),你可以通过 UTS__C__ 模块直接调用:

// index.uts  
export function loadModel(paramPath: string, binPath: string): boolean {  
  return __C__.yolo_load_model(paramPath, binPath);  
}  

export function detect(  
  rgbaData: Uint8Array,  
  width: number,  
  height: number,  
  maxObjects: number  
): Int32Array {  
  const outPoints = new Int32Array(maxObjects * 8); // 每个对象4点(x,y) => 8个int  
  const count = __C__.yolo_detect(  
    rgbaData,  
    width,  
    height,  
    outPoints,  
    maxObjects  
  );  
  return outPoints.subarray(0, count * 8);  
}  

export function rectify(  
  imgData: Uint8Array,  
  width: number,  
  height: number,  
  points: Int32Array,  
  maxBufferSize: number  
): { buffer: Uint8Array; outWidth: number; outHeight: number } {  
  const outBuffer = new Uint8Array(maxBufferSize);  
  const outW = new Int32Array(1);  
  const outH = new Int32Array(1);  
  __C__.yolo_rectify_by_points(  
    imgData,  
    width,  
    height,  
    points,  
    outBuffer,  
    maxBufferSize,  
    outW,  
    outH  
  );  
  return {  
    buffer: outBuffer,  
    outWidth: outW[0],  
    outHeight: outH[0]  
  };  
}

✅ 3. 可选:封装为插件接口(interface.uts)

如果你希望对外暴露类型清晰的接口,可在 interface.uts 中定义:

// interface.uts  
export type LoadModel = (paramPath: string, binPath: string) => boolean;  
export type Detect = (  
  rgbaData: Uint8Array,  
  width: number,  
  height: number,  
  maxObjects: number  
) => Int32Array;  
export type Rectify = (  
  imgData: Uint8Array,  
  width: number,  
  height: number,  
  points: Int32Array,  
  maxBufferSize: number  
) => { buffer: Uint8Array; outWidth: number; outHeight: number };

然后在 index.uts 中实现这些类型即可。


✅ 4. 使用示例(在页面中调用)

import { loadModel, detect, rectify } from '@/uni_modules/yolo-uts';  

// 示例调用  
const success = loadModel('/path/model.param', '/path/model.bin');

✅ 注意事项

  • .xcframework 必须包含 arm64 架构,且已签名。
  • 如果使用的是 Swift 封装层,需继承 UTSSwiftProtocol 并使用 @_silgen_name 暴露 C 函数。
  • 若出现链接错误,检查 config.json 是否正确声明了 frameworkssystemFrameworks

如需进一步调试,建议使用自定义基座运行并查看 Xcode 控制台输出。

内容为 AI 生成,仅供参考

要回复问题请先登录注册