Posted By

shadevampire on 06/03/11


Tagged

draw iphone CoreGraphics UIImage ios


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Tintenklecks
cnathan


draw to a UIImage with CoreGraphics


 / Published in: Objective C
 

URL: http://www.codza.com/creating-and-drawing-on-a-new-uiimage

input: width, height and inputImage output: outputImage

  1. CGFloat width, height; // max 1024 due to Core Graphics limitations
  2. UIImage *inputImage; // input image to be composited over new image as example
  3.  
  4. // create a new bitmap image context
  5. //
  6. UIGraphicsBeginImageContext(CGSizeMake(width, height));
  7.  
  8. // get context
  9. //
  10. CGContextRef context = UIGraphicsGetCurrentContext();
  11.  
  12. // push context to make it current
  13. // (need to do this manually because we are not drawing in a UIView)
  14. //
  15. UIGraphicsPushContext(context);
  16.  
  17. //
  18. // [drawing code comes here- look at CGContext reference
  19. // for available operations]
  20. //
  21.  
  22. // this example draws the inputImage into the context
  23. //
  24. [inputImage drawInRect:CGRectMake(0, 0, width, height)];
  25.  
  26.  
  27. // pop context
  28. //
  29. UIGraphicsPopContext();
  30.  
  31. // get a UIImage from the image context- enjoy!!!
  32. //
  33. UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
  34.  
  35. // clean up drawing environment
  36. //
  37. UIGraphicsEndImageContext();

Report this snippet  

You need to login to post a comment.