ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

OC 技术 C语言生成静态库给OC项目使用(源码)

OC 技术 C语言生成静态库给OC项目使用(源码) 一直觉得自己写的不是技术而是情怀一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的希望我的这条路能让你们少走弯路希望我能帮你们抹去知识的蒙尘希望我能帮你们理清知识的脉络希望未来技术之巅上有你们也有我。生成的库代码使用示例OC-libtiff静态库使用示例.zip文章目录说明软件说明生成步骤安装projcmakeautoconfautomakelibtool下载C语言的项目下来libtifflibgeotiff生成 configure 脚本新建文件编辑内容保存并退出在 vim 中输入 :wq修改文件权限的命令执行目录导入SDK位置导入XCode 使用导入SDK文件步骤 1添加头文件搜索路径步骤 2链接静态库步骤 3添加库文件步骤 4禁用 Bitcode不一定需要使用SDK打印命令查看静态库架构删除 brew uninstall proj libgeotiff libtiff安装 libgeotiff libtiff安装 proj安装 cmake说明2025.05.22 iOS开发TIF图片加载到高德地图尝试把C语言libtif库转换成iOS支持的x86_64,arm64静态库。第一次接触把操作方法记录下来第一重点如果下载下来的库操作一篇发现生成静态库失败就不要死重复步骤生成了重新下载解压过操作不然可能永远失败第二下载下来的C语言项目生成静态库有两种方式cmake和Autotools下面的教程是用Autotools的方式生成。软件说明工具名作用proj提供地理坐标转换功能libgeotiff 的必需依赖cmake用于构建某些依赖项如果它们使用 CMake 而非 Autotoolsautoconf生成 configure 脚本自动检测系统环境、生成 Makefile 的“生成器”automake生成标准的 Makefile.in 文件配合 autoconf 使用libtool帮助编译、链接静态库/动态库处理不同平台的差异生成步骤安装projcmakeautoconfautomakelibtool终端输入brew install autoconf automake libtool cmake proj下载C语言的项目下来libtiffhttps://download.osgeo.org/libtiff/终端输入cd ~终端输入git clone https://gitlab.com/libtiff/libtiff.git终端输入cd libtifflibgeotiffhttps://download.osgeo.org/geotiff/终端输入cd ~终端输入git clone https://github.com/OSGeo/libgeotiff.git终端输入cd libgeotiff生成 configure 脚本终端输入./autogen.sh新建文件终端输入touch build-tiff.sh编辑内容终端输入vim build-tiff.sh复制下面内容进去build-tiff.sh(成功)Autotools 的build-tiff.sh文件 arm64#!/bin/bash set -e echo 开始构建 libtiffarm64 静态库包括 libtiffxx... # 设置目标架构和安装目录 ARCHarm64 PLATFORMiphoneos SDK_PATH$(xcrun --sdk $PLATFORM --show-sdk-path) INSTALL_DIR$(pwd)/install # 清理旧的构建和安装目录 rm -rf $INSTALL_DIR mkdir -p $INSTALL_DIR # 设置环境变量编译器 flags export CC$(xcrun -sdk $PLATFORM -find clang) export CXX$(xcrun -sdk $PLATFORM -find clang) export CFLAGS-arch $ARCH -isysroot $SDK_PATH -miphoneos-version-min11.0 export CXXFLAGS$CFLAGS export LDFLAGS$CFLAGS # 执行 configure 脚本配置编译选项 echo 正在配置 libtiff... ./configure \ --hostarm-apple-darwin \ --enable-static \ --disable-shared \ --prefix$INSTALL_DIR \ --enable-cxxyes # 开始编译 echo 编译中... make -j$(sysctl -n hw.ncpu) # 安装到 install 目录 echo 安装到 $INSTALL_DIR ... make install echo ✅ 构建完成静态库位置$INSTALL_DIR/lib lipo -info $INSTALL_DIR/lib/libtiff.a lipo -info $INSTALL_DIR/lib/libtiffxx.a(成功)Autotools 的build-tiff.sh文件 x86_64#!/bin/bash set -e echo 开始构建 libtiffx86_64 静态库包括 libtiffxx... # 设置目标架构和平台 ARCHx86_64 PLATFORMiphonesimulator SDK_PATH$(xcrun --sdk $PLATFORM --show-sdk-path) INSTALL_DIR$(pwd)/install # 清理旧的构建和安装目录 rm -rf $INSTALL_DIR mkdir -p $INSTALL_DIR # 设置环境变量编译器 flags export CC$(xcrun -sdk $PLATFORM -find clang) export CXX$(xcrun -sdk $PLATFORM -find clang) export CFLAGS-arch $ARCH -isysroot $SDK_PATH -mios-simulator-version-min11.0 export CXXFLAGS$CFLAGS export LDFLAGS$CFLAGS # 执行 configure 脚本配置编译选项 echo 正在配置 libtiff... ./configure \ --hostx86_64-apple-darwin \ --enable-static \ --disable-shared \ --prefix$INSTALL_DIR \ --enable-cxxyes # 开始编译 echo 编译中... make -j$(sysctl -n hw.ncpu) # 安装到 install 目录 echo 安装到 $INSTALL_DIR ... make install # 打印架构信息 echo ✅ 构建完成静态库位置$INSTALL_DIR/lib lipo -info $INSTALL_DIR/lib/libtiff.a lipo -info $INSTALL_DIR/lib/libtiffxx.a保存并退出在 vim 中输入 :wq键盘按ESC终端输入:wq修改文件权限的命令终端输入chmod x build-tiff.sh执行目录终端输入./build-tiff.sh导入SDK位置导入XCode 使用导入SDK文件把生成的静态库复制到xcode项目里然后可以改一下文件的名字 我改为libtifSDK步骤 1添加头文件搜索路径Target→Build Settings→Search Paths→Header Search Paths添加路径确保递归搜索$(PROJECT_DIR)/libtifSDK/include步骤 2链接静态库Target→Build Settings→Search Paths→Library Search Paths添加路径$(PROJECT_DIR)/libtifSDK/lib步骤 3添加库文件Target→Build Phases→Link Binary With Libraries点击 →Add Other...→ 选择libtiff.a和libtiffxx.a步骤 4禁用 Bitcode不一定需要我没有操作该步骤但是打包可能报错再设置以前银行的SDK需要设置如果编译时未启用 Bitcode需在 Xcode 中关闭Build Settings → Enable Bitcode → NO使用SDK#import ViewController.h #import Masonry.h #include tiffio.h #include stdio.h // TIF图片投影信息 typedef struct { double imageWidth; // 图片宽度 double imageHeight; // 图片高度 double xPixelScale; // x轴像素 double yPixelScale; // y轴像素 double leftEasting; // 西侧(左)的东向坐标 double rightEasting; // 东侧(右)的东向坐标 double topNorthing; // 北侧(上)的北向坐标 double bottomNorthing; // 南侧(下)的北向坐标 int utmZone; // UTM 区域 (1-60) BOOL isNorthernHemisphere; // 是否北半球 (YES N, NO S) } TIFMESSAGE; interface ViewController () end implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor [UIColor whiteColor]; NSString *filePath [[NSBundle mainBundle] pathForResource:ESPG32649 ofType:tif]; //读取TIF图片信息 TIFMESSAGE tifmessage [self readTIFFMetadata:filePath]; } #pragma mark - 算法 - (TIFMESSAGE)readTIFFMetadata:(NSString *)filePath { TIFMESSAGE tifmessage {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, YES}; TIFF *tiff TIFFOpen([filePath UTF8String], r); if (tiff) { // 1. 读取基本图像信息 uint32_t width, height; TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, width); TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, height); tifmessage.imageWidth width; tifmessage.imageHeight height; NSLog(图片宽高: %dx%d, width, height); // 2. 读取像素比例yPixelScale double *pixelScale NULL; uint16_t pixelScaleCount 0; #define GEOTIFF_MODELPIXELSCALETAG 33550 if (TIFFGetField(tiff, GEOTIFF_MODELPIXELSCALETAG, pixelScaleCount, pixelScale) pixelScaleCount 2) { tifmessage.xPixelScale pixelScale[0]; tifmessage.yPixelScale pixelScale[1]; // 先保留原始值 NSLog(原始像素大小: X%f, Y%f, tifmessage.xPixelScale, tifmessage.yPixelScale); } // 3. 读取控制点 double *tiePoints NULL; uint16_t tiePointCount 0; #define GEOTIFF_MODELTIEPOINTTAG 33922 if (TIFFGetField(tiff, GEOTIFF_MODELTIEPOINTTAG, tiePointCount, tiePoints) tiePointCount 6) { tifmessage.leftEasting tiePoints[3]; tifmessage.topNorthing tiePoints[4]; NSLog(原点 (geo coords): 左上角 X%f, Y%f, tifmessage.leftEasting, tifmessage.topNorthing); } // 4. 读取 GeoKeys 以确认坐标系类型、方向和UTM区域 uint16_t *geoKeyDirectory NULL; uint16_t geoKeyCount 0; #define GEOTIFF_GEOKEYDIRECTORYTAG 34735 if (TIFFGetField(tiff, GEOTIFF_GEOKEYDIRECTORYTAG, geoKeyCount, geoKeyDirectory)) { uint16_t numKeys geoKeyDirectory[3]; // Key的数量 BOOL isProjected NO; BOOL isRasterPixelIsArea NO; for (int i 0; i numKeys; i) { uint16_t *keyEntry geoKeyDirectory 4 i * 4; uint16_t keyID keyEntry[0]; uint16_t location keyEntry[1]; uint16_t count keyEntry[2]; uint16_t value keyEntry[3]; switch (keyID) { case 1024: // GTModelTypeGeoKey isProjected (value 1); // 1Projected, 2Geographic break; case 1025: // GTRasterTypeGeoKey isRasterPixelIsArea (value 1); // 1Area, 2Point break; case 3072: { // ProjectedCSTypeGeoKey。 32649 // EPSG 编码规则: // - 32601-32660: WGS84 / UTM zone 1N-60N // - 32701-32760: WGS84 / UTM zone 1S-60S // - 32201-32260: 传统GeoTIFF UTM zone 1N-60N (较少见) // - 32301-32360: 传统GeoTIFF UTM zone 1S-60S (较少见) if (value 32601 value 32660) { // WGS84北半球UTM (EPSG:326xx) tifmessage.utmZone value - 32600; tifmessage.isNorthernHemisphere YES; } else if (value 32701 value 32760) { // WGS84南半球UTM (EPSG:327xx) tifmessage.utmZone value - 32700; tifmessage.isNorthernHemisphere NO; } else if (value 32201 value 32260) { // 传统GeoTIFF北半球UTM tifmessage.utmZone value - 32200; tifmessage.isNorthernHemisphere YES; } else if (value 32301 value 32360) { // 传统GeoTIFF南半球UTM tifmessage.utmZone value - 32300; tifmessage.isNorthernHemisphere NO; } if (tifmessage.utmZone 0) { NSLog(ProjectedCSTypeGeoKey: %d → UTM Zone: %d%s, value, tifmessage.utmZone, tifmessage.isNorthernHemisphere ? N : S); } else { NSLog(ProjectedCSTypeGeoKey: %d (非UTM坐标系), value); } break; } } } if (tifmessage.utmZone 0) { NSLog(UTM Zone: %d%s, tifmessage.utmZone, tifmessage.isNorthernHemisphere ? N : S); } // 5. 动态判断 yPixelScale 的符号 if (isProjected) { // 投影坐标系如UTM通常 Y 轴朝北为正但图像存储可能是从上到下Y递减 // 如果 RasterPixelIsArea通常 yPixelScale 应为负 if (isRasterPixelIsArea) { tifmessage.yPixelScale -fabs(tifmessage.yPixelScale); NSLog(投影坐标系 RasterPixelIsArea → 修正 Y 方向为负: %f, tifmessage.yPixelScale); } else { // 如果是 RasterPixelIsPoint可能需要进一步检查 NSLog(投影坐标系 RasterPixelIsPoint → 保留原始 Y 方向: %f, tifmessage.yPixelScale); } } else { // 地理坐标系如WGS84根据实际数据约定 NSLog(地理坐标系 → 保留原始 Y 方向: %f, tifmessage.yPixelScale); } } // 6. 检查 ModelTransformationTag更精确的变换矩阵 double *transformMatrix NULL; uint16_t transformCount 0; #define GEOTIFF_MODELTRANSFORMATIONTAG 34264 if (TIFFGetField(tiff, GEOTIFF_MODELTRANSFORMATIONTAG, transformCount, transformMatrix) transformCount 16) { // transformMatrix 是 4x4 矩阵其中 [5] 是 Y 方向的缩放因子 double yScale transformMatrix[5]; if (yScale ! 0) { tifmessage.yPixelScale yScale; NSLog(ModelTransformationTag 覆盖 Y 方向: %f, tifmessage.yPixelScale); } } TIFFClose(tiff); } return tifmessage; } end打印命令下面介绍其他的常用命令查看静态库架构lipo -info /usr/local/lib/libgeotiff.dyliblipo -info /usr/local/lib/libtiff.dylib删除 brew uninstall proj libgeotiff libtiffbrew uninstall proj libgeotiff libtiff安装 libgeotiff libtiff安装下来的也是 mac 的x86_64架构 iOS用不上arch -arm64 brew install libgeotiff libtiff安装 projbrew install proj安装 cmakebrew install cmake
返回列表