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

來源:互聯網
上載者:User

http://www.gotow.net/creative/wordpress/?p=33

 

 

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。我的方法會更簡單。

 

UIImage* UIImageFromLayer(CGLayerRef layer)<br />{<br /> CGContextRef ctx = CGLayerGetContext(layer);<br /> CGImageRef img = CGBitmapContextCreateImage(bitmapContext);<br /> UIImage* ui_img = [UIImage imageWithCGImage: img];<br />} 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.