ios-图片类型的上下文

图片类型的上下文可以帮我们获取图片,和layer类型的上下文是不一样的,图片类型的上下文渲染是渲染到一张图片上,而layer类型的上下文是直接渲染到了UIView上了。

我们对于图片类型的上下文我们首先要开启,结束之后别忘记关闭。大致的步骤如下所示。

 //开启图片类型的上下文
    UIGraphicsBeginImageContext(CGSizeMake(300, 300));
    
    //获取当前的上下文(图片类型)
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    
    //拼接路径,同时把理解添加到上下文中
    CGContextMoveToPoint(ctx, 50, 50);
    CGContextAddLineToPoint(ctx, 100, 100);
    
    //渲染
    CGContextStrokePath(ctx);
    
    //通过图片类型的上下文去获取图片的对象
    UIImage * image=UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭图片类型的上下文
    UIGraphicsEndImageContext();
    
    //把获取到的图片放到图片框上
    self.imageView.image=image;
把图片保存到沙盒中
//获取Document的路径
    NSString * documentPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    //获取文件的路径
    NSString * fliePath=[documentPath stringByAppendingPathComponent:@"hh.png"];
    //把image的对象转换成NSData
    NSData * data=UIImagePNGRepresentation(image);
    
    //通过data的Write to file方法写到沙盒中
    [data writeToFile:fliePath atomically:YES];
    NSLog(@"%@",fliePath);

UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 300), NO, 0);这个参数也可以开始图片的上下文类型,第二个参数是设置透不透明,YES就是为不透明,反之就是透明,第三个参数是缩放的比例,如果我们传0就默认的去用屏幕的缩放因子  [UIScreen mainScreen].scale,。上面的那个获取上下文的方法相当于下面的获取上下文方法的第二个参数为NO,第三个参数为1。

关于UIGraphicsBeginImageContextWithOptions函数,官方文档的解释是

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 (kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host). Otherwise, each pixel uses a premultipled ARGB format (kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host).

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. 

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 UIGraphicsEndImageContextfunction 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.

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. 

This function may be called from any thread of your app.

大致的意思就是

您可以使用这个函数来将绘制环境配置为一个位图。位图的格式是一个使用主机字节顺序的ARGB 32位整数像素格式。如果不透明的参数是肯定的,alpha通道被忽略和位图被视为完全不透明(kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host)。否则,每个像素使用premultipled ARGB格式(kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host)。

环境还使用了UIKit视图的默认坐标系统,该系统的原点位于左上角,正轴向下延伸到原点的右侧。所提供的比例因子也适用于坐标系统和结果图像。绘图环境立即被推送到图形上下文堆栈中。

当上下文由这个函数是当前上下文,你可以叫UIGraphicsGetImageFromCurrentImageContext函数来检索图像对象基于上下文的当前内容。
当你完成修改上下文,您必须调用UIGraphicsEndImageContext函数清理位图绘制环境和删除图形上下文从上下文堆栈的顶部。您不应该使用UIGraphicsPopContext函数从堆栈中删除这类上下文。

在大多数其他方面,由这个函数创建的图形上下文与其他任何图形上下文一样。您可以通过推动和弹出其他图形上下文来更改上下文。你也可以通过UIGraphicsGetCurrentContext函数得到位图上下文使用。

这个函数可以从应用程序的任何线程调用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值