How to create a blank transparent png in Objective-C?

Objective CIosUiimage

Objective C Problem Overview


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

Thanks :)

Objective C Solutions


Solution 1 - Objective C

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();

Solution 2 - Objective C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNiliView Question on Stackoverflow
Solution 1 - Objective CSergey KalinichenkoView Answer on Stackoverflow
Solution 2 - Objective CDawnSongView Answer on Stackoverflow