目录:
1、内存中绘图得到PNG图片
2、由三张已知图片拼得一张新图片
内存中绘图得到PNG图片(绘制好的图片可以贴到imageview上,背景是透明的,不影响其他内容显示)
- (UIImage*)drawImage:(NSArray *)arr
{
//这个是根据数组arr中传入的点的坐标画图
// 创建内存中的图片(输入图片的宽和高)
UIGraphicsBeginImageContext(CGSizeMake(MWIDTH, 90));
// ---------下面开始向内存中绘制图形---------
//这里使用贝塞尔曲线画图
UIBezierPath *bezierPath2 = [[UIBezierPath alloc]init];
for (int i = 0; i < 8; i ++)
{
NSString *x = arr[i][@"x"];
NSString *y = arr[i][@"y"];
UIBezierPath *bezierPath1 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(x.floatValue, y.floatValue, 2, 2)];
//使用1画红色圆点
bezierPath1.lineWidth = 2;
[[UIColor redColor] setStroke];
[bezierPath1 stroke];
if (i == 0) {
[bezierPath2 moveToPoint:CGPointMake(x.floatValue, y.floatValue+1)];
}else{
[bezierPath2 addLineToPoint:CGPointMake(x.floatValue, y.floatValue+1)];
}
//使用2画橘色连线
}
bezierPath2.lineWidth = 1;
[[UIColor orangeColor] setStroke];
[bezierPath2 stroke];
// 获取该绘图Context中的图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// ---------结束绘图---------
UIGraphicsEndImageContext();
// 获取当前应用路径中Documents目录下的指定文件名对应的文件路径
NSString *path = [[NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"]
stringByAppendingPathComponent:@"newPng.png"];
NSLog(@"Path == %@",path);
// 保存PNG图片
[UIImagePNGRepresentation(newImage) writeToFile:path atomically:YES];
//可以通过打印的路径在文件夹中查看绘制的图片
return newImage;
}
2、由三张已知图片拼得一张新图片
/**
* image拼图 由上、中、下三张图片合并成一张图片
*
* @param upImage 上面的图片
* @param midImage 中间的图片
* @param downImage 底部的图片
* @param resultSize 需要返回的合并后的图片的大小 注意不要小于三张图片加起来的最小size
*
* @return
*/
+ (UIImage*)getImageFromUpImage:(UIImage *)upImage
midImage:(UIImage *)midImage
downImage:(UIImage *)downImage
withSize:(CGSize)resultSize
{
UIGraphicsBeginImageContextWithOptions(resultSize, NO, 0.0);
[upImage drawInRect:CGRectMake(0, 0,resultSize.width, upImage.size.height)];
[midImage drawInRect:CGRectMake(0, upImage.size.height,resultSize.width, resultSize.height - upImage.size.height-downImage.size.height)];
[downImage drawInRect:CGRectMake(0, resultSize.height - downImage.size.height,resultSize.width, downImage.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}