//1.获取图片UIImage *image = [UIImage imageNamed:@"123.jpg"];//处理图片//2.获取图片的长度long length = image.size.width/4;//3.图片顶点索引long indices[] = {length * 2,length,//右 right0,length,//左 leftlength,0,//上 toplength,length * 2,//底 bottomlength,length,//顶 frontlength * 3 ,length,//背面 back};//4.指定图片个数//为什么除以2? 包含了x,ylong faceCount = sizeof(indices)/sizeof(indices[0])/2;//4.获取图片大小 单个图片大小:length * length ,组合起来:length,length * faceCountCGSize 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转换为UIImageUIImage *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{}