Creating a UIImage from a CGLayer ,通过CGLayer穿件UIImage

CGLayers are great for drawing - especially when things need to be drawn over and over again. Converting a CGLayer to a UIImage is another story, though. NetSketch uses CGLayers for the drawing canvas, but converts them to UIImages when you go to upload your drawing or email it to a friend. The code below shows how it’s done. The CGLayer is drawn into a bitmap CGContext of the same size, and then a CGImage is created around the CGContext. The CGImage can be turned into a UIImage, and you’re done!

Be sure to leave a comment if you find this function useful!

  1. UIImage* UIImageFromLayer(CGLayerRef layer)  
  2. {  
  3.     // Create the bitmap context  
  4.     CGContextRef    bitmapContext = NULL;  
  5.     void *          bitmapData;  
  6.     int             bitmapByteCount;  
  7.     int             bitmapBytesPerRow;  
  8.     CGSize          size = CGLayerGetSize(layer);  
  9.   
  10.     // Declare the number of bytes per row. Each pixel in the bitmap in this  
  11.     // example is represented by 4 bytes; 8 bits each of red, green, blue, and  
  12.     // alpha.  
  13.     bitmapBytesPerRow   = (size.width * 4);  
  14.     bitmapByteCount     = (bitmapBytesPerRow * size.height);  
  15.   
  16.     // Allocate memory for image data. This is the destination in memory  
  17.     // where any drawing to the bitmap context will be rendered.  
  18.     bitmapData = malloc( bitmapByteCount );  
  19.     if (bitmapData == NULL)  
  20.     {  
  21.         return nil;  
  22.     }  
  23.   
  24.     // Create the bitmap context. We want pre-multiplied ARGB, 8-bits  
  25.     // per component. Regardless of what the source image format is  
  26.     // (CMYK, Grayscale, and so on) it will be converted over to the format  
  27.     // specified here by CGBitmapContextCreate.  
  28.     bitmapContext = CGBitmapContextCreate (bitmapData, size.width, size.height,8,bitmapBytesPerRow,  
  29.                                         CGColorSpaceCreateDeviceRGB(),kCGImageAlphaNoneSkipFirst);  
  30.   
  31.     if (bitmapContext == NULL)  
  32.         // error creating context  
  33.         return nil;  
  34.   
  35.     CGContextScaleCTM(bitmapContext, 1, -1);  
  36.     CGContextTranslateCTM(bitmapContext, 0, -size.height);  
  37.   
  38.     // Draw the image to the bitmap context. Once we draw, the memory  
  39.     // allocated for the context for rendering will then contain the  
  40.     // raw image data in the specified color space.  
  41.     CGContextDrawLayerAtPoint(bitmapContext, CGPointZero, layer);  
  42.     CGImageRef   img = CGBitmapContextCreateImage(bitmapContext);  
  43.     UIImage*     ui_img = [UIImage imageWithCGImage: img];  
  44.   
  45.     CGImageRelease(img);  
  46.     CGContextRelease(bitmapContext);  
  47.     free(bitmapData);  
  48.   
  49.     return ui_img;  
  50.   
  51. }  

总体意思是:把CGLayer画在新建的CGContext上,然后再转换为UIImage。我的方法会更简单。

 

  1. UIImage* UIImageFromLayer(CGLayerRef layer)   
  2. {  
  3.    CGContextRef ctx = CGLayerGetContext(layer);  
  4.    CGImageRef   img =      CGBitmapContextCreateImage(bitmapContext);    
  5.    UIImage*     ui_img = [UIImage imageWithCGImage: img];    
  6. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值