Creating a UIImage from a CGLayer, using CGLayer to wear UIImage

Source: Internet
Author: 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 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. // Declare the number of bytes per row. Each pixel in the bitmap in this
  10. // Example is represented by 4 bytes; 8 bits each of red, green, blue, and
  11. // Alpha.
  12. BitmapBytesPerRow = (size. width * 4 );
  13. BitmapByteCount = (bitmapBytesPerRow * size. height );
  14. // Allocate memory for image data. This is the destination in memory
  15. // Where any drawing to the bitmap context will be rendered.
  16. BitmapData = malloc (bitmapByteCount );
  17. If (bitmapData = NULL)
  18. {
  19. Return nil;
  20. }
  21. // Create the bitmap context. We want pre-multiplied ARGB, 8-bits
  22. // Per component. Regardless of what the source image format is
  23. // (CMYK, Grayscale, and so on) it will be converted over to the format
  24. // Specified here by CGBitmapContextCreate.
  25. BitmapContext = CGBitmapContextCreate (bitmapData, size. width, size. height, 8, bitmapBytesPerRow,
  26. CGColorSpaceCreateDeviceRGB (), kCGImageAlphaNoneSkipFirst );
  27. If (bitmapContext = NULL)
  28. // Error creating context
  29. Return nil;
  30. CGContextScaleCTM (bitmapContext, 1,-1 );
  31. CGContextTranslateCTM (bitmapContext, 0,-size. height );
  32. // Draw the image to the bitmap context. Once we draw, the memory
  33. // Allocated for the context for rendering will then contain
  34. // Raw image data in the specified color space.
  35. CGContextDrawLayerAtPoint (bitmapContext, CGPointZero, layer );
  36. CGImageRef img = CGBitmapContextCreateImage (bitmapContext );
  37. UIImage * ui_img = [UIImage imageWithCGImage: img];
  38. CGImageRelease (img );
  39. CGContextRelease (bitmapContext );
  40. Free (bitmapData );
  41. Return ui_img;
  42. }

In general, CGLayer is painted on the newly created CGContext and then converted to UIImage. My method will be simpler.

 

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.