//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{
}
?? ?