UIGraphicsBeginImageContextWithOptions

首先说下下面这个函数
void UIGraphicsBeginImageContext(CGSize size)
创建一个基于位图的上下文,并将其设置为当前的上下文.
参数size 为新创建的位图上下文的大小,它同时是由UIGraphicsGetImageFromCurrentImageContext函数返回的图形大小
该函数的功能同
UIGraphicsBeginImageContextWithOptions
的功能相同,相等于其opaque参数为NO,scale因子为1.0
函数原型为:

void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);

size: 同UIGraphicsBeginImageContext
opaque:透明开关,如果图形完全不用透明,设置为YES以优化位图的存储。
scale:缩放因子,iPhone 4是2.0,其他是1.0。虽然这里可以用[UIScreen mainScreen].scale来获取,但实际上设为0后,系统就会自动设置正确的比例了.

接下来的drawInRect:把图像画到了当前的image context里,这时就完成了解压缩和重采样的工作了。然后再从image context里获取新的image,这个image的缩放比例也能正确地和设备匹配。

下面分析下函数的英文注释
void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);

  • Creates a bitmap-based graphics context with the specified options.
    //使用指定的选项创建基于位图的图形上下文。
  • You use this function to configure the drawing environment for rendering into a bitmap. The format for the bitmap is a ARGB 32-bit integer pixel format using host-byte order. If the opaque parameter is YES, the alpha channel is ignored and the bitmap is treated as fully opaque Otherwise, each pixel uses a premultipled ARGB format
    //您可以用这个函数为图像渲染到位图配置绘图环境,位图的格式是使用主机字节顺序的ARGB 32位整数像素格式,如果opaque参数为YES,则忽略alpha通道,并将位图视为完全不透明。否则,每个像素使用预乘的ARGB格式.

  • The environment also uses the default coordinate system for UIKit views, where the origin is in the upper-left corner and the positive axes extend down and to the right of the origin. The supplied scale factor is also applied to the coordinate system and resulting images. The drawing environment is pushed onto the graphics context stack immediately.
    //环境还使用UIKit视图的默认坐标系,其中原点位于左上角,以原点为基础向下和右侧为正值。提供的比例因子也应用于坐标系和结果图像。绘图环境立即被推送到图形上下文堆栈中。

  • While the context created by this function is the current context, you can call the UIGraphicsGetImageFromCurrentImageContext function to retrieve an image object based on the current contents of the context. When you are done modifying the context, you must call the UIGraphicsEndImageContext function to clean up the bitmap drawing environment and remove the graphics context from the top of the context stack. You should not use the UIGraphicsPopContext function to remove this type of context from the stack.
    // 虽然此函数创建的上下文是当前上下文,但您可以调用UIGraphicsGetImageFromCurrentImageContext函数取回以基于当前上下文内容的图像对象。完成上下文修改后,必须调用UIGraphicsEndImageContext函数从上下文堆栈顶部删除图形上下文并清除位图绘制环境。,您不要使用UIGraphicsPopContext函数从堆栈中删除此类上下文.

  • In most other respects, the graphics context created by this function behaves like any other graphics context. You can change the context by pushing and popping other graphics contexts. You can also get the bitmap context using the UIGraphicsGetCurrentContext function.
    //在大多数其他方面,此函数创建的图形上下文的行为与任何其他图形上下文相同。您可以通过push和pop其他图形上下文来更改上下文。您还可以使用UIGraphicsGetCurrentContext函数获取位图上下文

  • This function may be called from any thread of your app.
    // 可以从应用程序的任何线程调用此函数

  • 参数
    size:
    The size (measured in points) of the new bitmap context. This represents the size of the image returned by the UIGraphicsGetImageFromCurrentImageContext function. To get the size of the bitmap in pixels, you must multiply the width and height values by the value in the scale parameter.
    //新位图上下文的大小(以点为单位)。这表示UIGraphicsGetImageFromCurrentImageContext函数返回的图像的大小。要以像素为单位获取位图的大小,必须将width和height值乘以scale参数中的值。
    opaque:
    A Boolean flag indicating whether the bitmap is opaque. If you know the bitmap is fully opaque, specify YES to ignore the alpha channel and optimize the bitmap’s storage. Specifying NO means that the bitmap must include an alpha channel to handle any partially transparent pixels.
    //一个布尔标志,指示位图是否不透明。如果您知道位图是完全不透明的,请指定YES以忽略Alpha通道并优化位图的存储。指定NO意味着位图必须包含alpha通道以处理任何部分透明的像素。
    scale:
    The scale factor to apply to the bitmap. If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.
    //要应用于位图的比例因子。如果默认值0.0,则比例因子将设置为设备主屏幕的比例因子。
    总结

    //  初始化modifiedImage为保存绘制出来的image
     UIImage *modifiedImage;
    //  初始化rect
      CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    // 1 开始创建绘图上下文    
    UIGraphicsBeginImageContextWithOptions(image.size, false, [[UIScreen mainScreen] scale]);
    // 用圆角剪切rect
    [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:8.0] addClip];
    // 2 将图像绘制到rect中   
    [image drawInRect:rect];
    // 3 从当前上下文中获取图像
     modifiedImage = UIGraphicsGetImageFromCurrentImageContext();
    // 4 结束绘制
    UIGraphicsEndImageContext();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值