美文网首页
创建图形上下文的方法

创建图形上下文的方法

作者: Pierce_蛋 | 来源:发表于2019-02-27 16:57 被阅读0次

1. 通过CGContext的方法创建

/**
  *  通过图片获得和图片大小一样的画布
  *
  *  @param inImage
  *
  *  @return 返回画布对象
  */
 - (CGContextRef)createARGBBitmapContextFromImage:(CGImageRef)inImage {
     CGContextRef    context = NULL;
     CGColorSpaceRef colorSpace;
     void *          bitmapData;
     size_t            bitmapByteCount;
     size_t            bitmapBytesPerRow;
    
     // Get image width, height. We'll use the entire image.
     size_t pixelsWide = CGImageGetWidth(inImage);
     size_t pixelsHigh = CGImageGetHeight(inImage);
    
     // Declare the number of bytes per row. Each pixel in the bitmap in this
     // example is represented by 4 bytes; 8 bits each of red, green, blue, and
     // alpha.
     bitmapBytesPerRow   = (pixelsWide * 4);
     bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);
    
     // Use the generic RGB color space.
     colorSpace = CGColorSpaceCreateDeviceRGB();
    
     if (colorSpace == NULL)
     {
         fprintf(stderr, "Error allocating color space\n");
         return NULL;
     }
    
     // Allocate memory for image data. This is the destination in memory
     // where any drawing to the bitmap context will be rendered.
     bitmapData = malloc( bitmapByteCount );
     if (bitmapData == NULL)
     {
         fprintf (stderr, "Memory not allocated!");
         CGColorSpaceRelease( colorSpace );
         return NULL;
     }
    
     // Create the bitmap context. We want pre-multiplied ARGB, 8-bits
     // per component. Regardless of what the source image format is
     // (CMYK, Grayscale, and so on) it will be converted over to the format
     // specified here by CGBitmapContextCreate.
     context = CGBitmapContextCreate (bitmapData,
                                      pixelsWide,
                                      pixelsHigh,
                                      8,      // bits per component
                                      bitmapBytesPerRow,
                                      colorSpace,
                                      kCGImageAlphaPremultipliedFirst);
     if (context == NULL) {
         free (bitmapData);
         fprintf (stderr, "Context not created!");
     }
     // Make sure and release colorspace before returning
     CGColorSpaceRelease( colorSpace );
     return context;
}

2. 通过UIGraphicsBeginImageContextWithOptions的方法创建

/**
   *  主要用来创建一个基于位图的图形上下文(这里称之为画布)
   *
   *  @param size   在调用UIGraphicsGetImageFromCurrentImageContext 时返回的画布的尺寸,这个尺寸是以像素点的方式返回,所以实际宽高的输出是size的值乘上后面提供的scale参数
   *  @param opaque 布尔值,表示图像是否不透明
   *  @param scale  生成bitmap的比例因子,如果设置的值为0的话表示和屏幕的scale一样
   */
 UIGraphicsBeginImageContextWithOptions(size, 0, 0);
 //获得当前画布
 CGContextRef context = UIGraphicsGetCurrentContext();
 //结束当前的画布操作
 UIGraphicsEndImageContext();

注:在iOS中,最好用UIGraphicsBeginImageContextWithOptions来代替底层的CGContext。因为如果你用CGContext的方式创建一个画布(原文:offscreen bitmap,这里理解为一个画布)的话,创建出来的画布的坐标系为bitmap graphics context的方式的,而如果你用UIGraphicsBeginImageContextWithOptions创建画布的话,创建出来的画布坐标系是UIView方式的,这两个坐标系Y轴的坐标是刚好相反的。虽然你可以在最后要显示出来的时候手动转换坐标系为UIView的方式,但是明显这样的性能就不是最佳的了。

相关文章

  • iOS 图片压缩和剪裁

    创建BitMap图形上下文的方法: 讲解上面的四个方法: UIGraphicsBeginImageContext ...

  • canvas-裁切clip()

    使用图形上下文不带参数的clip()方法来实现Canvas图形裁切功能,该方法会使用先创建好的路径对canvas设...

  • iOS绘图上下文的理解

    在调用drawRect:方法之前,绘图系统创建了一个图形上下文(CGContext)。上下文包括大量信息,比如画笔...

  • 创建图形上下文的方法

    1. 通过CGContext的方法创建 2. 通过UIGraphicsBeginImageContextWithO...

  • CGContextSaveGState与UIGraphicsP

    在调用drawRect:方法之前,绘图系统创建了一个图形上下文CGContext。上下文包括大量信息,比如画笔颜色...

  • CGContextSaveGState和CGContextRes

    使用Quartz时涉及到一个图形上下文,其中图形上下文中包含一个保存过的图形状态堆栈。在Quartz创建图形上下文...

  • IOS 中 给PDF 绘制内容

    绘制到PDF则要启用pdf图形上下文,PDF图形上下文的创建使用方式跟位图图形上下文是类似的,需要注意的一点就是绘...

  • 绘制饼状图、柱状图和进度条

    绘制图形 基本步骤: 获取图形上下文 创建路径 将路径添加到上下文中 渲染当然,操作需要在view的drawRec...

  • iOS动画 —— 自定义View

    创建路径 1.使用CGContextRef创建,如CGContextAddArc 这种方式是直接对图形上下文进行操...

  • CoreGraphics

    图形上下文 Core Graphics 使用图形上下文进行工作,这个上下文的作用像画家的画布一样。在图形上下文之外...

网友评论

      本文标题:创建图形上下文的方法

      本文链接:https://www.haomeiwen.com/subject/ltifuqtx.html