UIImage

目录:
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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这篇文章是笔者在开发App过程中发现的一些内存问题, 然后学习了YYKit框架时候也发现了图片的缓存处理的 不够得...
    Magic_Unique阅读 2,700评论 5 8
  • 前言 曾经做过一个图片编辑软件,对图片进行添加贴纸 滤镜等功能,其中遇到一个问题,因为对图片质量要求较高,需要对原...
    ashura_阅读 1,536评论 0 5
  • 官方文档点蓝色文字:UIImage、CIImage、CGImage。 这篇文章是对官方文档的学习笔记,不是翻译,对...
    阿斯兰iOS阅读 6,527评论 1 8
  • 后继更新... 2016-05-24 UIImage 概述: UIImage对象在App中管理图片数据. 使用Im...
    没刀的大佐阅读 10,720评论 9 23
  • 在iOS绘图 - 基础篇中我们知道有一种获取上下文的方法是UIGraphicsBeginImageContextW...
    莫须有恋阅读 17,780评论 4 51