ios 裁剪拼装图片

复制代码
    //1.获取图片
    UIImage *image = [UIImage imageNamed:@"123.jpg"];

    //处理图片
    //2.获取图片的长度
    long length = image.size.width/4;
    
    //3.图片顶点索引
    long indices[] = {
        length * 2,length,//右 right
        0,length,//左 left
        length,0,//上 top
        length,length * 2,//底 bottom
        length,length,//顶 front
        length * 3 ,length,//背面 back
    };
    
    //4.指定图片个数
    //为什么除以2? 包含了x,y
    long faceCount = sizeof(indices)/sizeof(indices[0])/2;
    
    //4.获取图片大小 单个图片大小:length * length ,组合起来:length,length * faceCount
    CGSize imageSize = {length,length * faceCount};
    
    //5.创建基于位图的图形上下文并使其成为当前上下文。
    UIGraphicsBeginImageContext(imageSize);
    
    for (int i = 0; i + 2 <= faceCount * 2 ; i += 2) {
        
        //创建图片,从image.CGImage,获取位置rect上的图片
        CGImageRef CGImage = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(indices[i], indices[i+1], length, length));
        
        //将CGImage转换为UIImage
        UIImage *tmp = [UIImage imageWithCGImage:CGImage];
        
        //绘制图片
        [tmp drawInRect:CGRectMake(0, length * i / 2, length, length)];
        
    }
    
    //5.获取处理好的图片
    //UIGraphicsGetImageFromCurrentImageContext 基于当前位图基于图形上下文的内容返回图像。只有在基于位图的图形上下文是当前图形上下文时,才应该调用此函数。如果目前的情况下是零或没有通过UIGraphicsBeginImageContext创建,这个函数返回nil。
    UIImage *finalImg = UIGraphicsGetImageFromCurrentImageContext();
        //6.保存图片到沙盒
    //指定图片路径
    NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image123.png"];
    //获取图片数据-png格式的图片数据
    //UIImagePNGRepresentation(imgData)以PNG格式返回指定图像的数据
    NSData *imageData = UIImagePNGRepresentation(finalImg);
    
    //写入文件
    BOOL writeStatus = [imageData writeToFile:path atomically:YES];
    
    if (writeStatus) {
    
    }else{

    }
相关推荐
ZZH_AI项目交付19 小时前
同一套前摄心率算法,为什么 iPhone 16 Pro 稳,iPhone XR 会失准?
ios·app·ai编程
DeMinds19 小时前
内容没有丢,我为什么总在重新整理?|DeMinds 如何让工作接着继续
ios·github·markdown
apd_csdn20 小时前
清华邮箱苹果邮件app设置(全)
ios·thu
寒水馨20 小时前
macOS下载、安装openclaw-v2026.7.1(附安装包OpenClaw-2026.7.1.dmg)
macos·大模型·github·开源软件·ai助手·openclaw·gpt-5.6
MDM.Plus21 小时前
从“遥控”到“自治”:苹果 MDM 技术的代际跨越与业务重构
ios·智能手机·重构·mdm
黑化旺仔21 小时前
iOS - 天气预报仿写总结
ios
Coffeeee1 天前
ios零基础的Android开发能否靠AI让老板省一笔人工费呢
android·ios·ai编程
2501_916007471 天前
深入理解HTTPS对称与非对称加密机制及Charles抓包实践
网络协议·http·ios·小程序·https·uni-app·iphone
2501_916008891 天前
HTTPS 抓包遇到证书绑定怎么办,使用 TraceEagle 解除 App 证书校验
网络协议·计算机网络·http·网络安全·ios·adb·https
白玉cfc1 天前
【iOS】MRC和ARC
macos·ios·cocoa