3

If an image context is created using:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(768, 768), YES, 1.0);

is there a way to get the size of the context later on? The size for sure is there when the image is obtained from the context, but while drawing, is there also a way to get the width and size of it?

Jeremy L
  • 3,770
  • 6
  • 41
  • 62

1 Answers1

2

For a bitmap context as above you can use:

CGContextRef context = UIGraphicsGetCurrentContext();
size_t width = CGBitmapContextGetWidth(context);
size_t height = CGBitmapContextGetHeight(context);
Simon Lawrence
  • 564
  • 2
  • 8
  • works with ImageContext... and I guess supposedly it won't work with a context that is a CGLayer of size (768, 768) – Jeremy L May 28 '12 at 09:47
  • (coz it would print out error even thought that CGLayer was created with a size (768, 768)) – Jeremy L May 29 '12 at 00:45
  • This exact code returns 0 for me (both for width and for height). I thought that's because the UIGraphicsGetCurrentContext doesn't return a bitmap context. – Noam Ohana Jul 28 '22 at 13:13