52

How can I create a blank png UIImage in Objective C programmatically (let's say 36x36 px)?

Thanks :)

Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129
Nili
  • 1,963
  • 3
  • 20
  • 39

2 Answers2

104

You can open an image context, and then harvest its content immediately, without drawing anything into it. This should produce a blank 36x36 UIImage:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(36, 36), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0
let img = UIImage() // size does not matter for a blank transparent image

When using a blank transparent image, its size does not matter, and I think the imageView.contentMode might be any scalable value, or set it to UIView.ContentMode.scaleAspectFit or UIView.ContentMode.scaleAspectFill if needed.

DawnSong
  • 4,752
  • 2
  • 38
  • 38