IPhone: How to Add White Background to PNG

Calgary Jul 30, 2026

Have you ever found yourself in a situation where you needed to use a PNG image with a transparent background, but the iOS app you're working with insists on displaying it against a solid white background? This can be quite frustrating, especially when you've designed your PNG image to blend seamlessly with its surroundings. But fear not, for Apple has provided a solution through the UIGraphicsBeginImageContextWithOptions method. Let's delve into how you can use it to add a white background to your PNG image on iPhone.

Iphone kecil
Iphone kecil

The UIGraphicsBeginImageContextWithOptions method is a powerful tool that allows you to create an image context with the specified size and scale. By using this method, you can draw your image with a white background and then retrieve it using UIGraphicsGetImageFromCurrentImageContext. This way, you ensure that your image always appears with a white background, regardless of the view it's displayed in.

an iphone on a white background, with no image in the top right hand corner
an iphone on a white background, with no image in the top right hand corner

Using UIGraphicsBeginImageContextWithOptions

Before we dive into the implementation, let's understand the parameters of the UIGraphicsBeginImageContextWithOptions method. It takes three arguments: the size of the context, the scale, and the opacity. For adding a white background to your PNG image, you can use the size of your image, a scale of 0.0, and an opacity of 1.0. The scale of 0.0 ensures that the context is not scaled, and the opacity of 1.0 ensures that the context is fully opaque.

the new iphone 11 is shown in white
the new iphone 11 is shown in white

Furthermore, it's important to note that you should always use UIGraphicsEndImageContext after calling UIGraphicsBeginImageContextWithOptions. This cleans up the graphic context and releases any resources it was using. If you forget to do this, you might encounter performance issues in your app.

Step-by-Step Implementation

an iphone with a blank screen on the front and back side, sitting in front of a white background
an iphone with a blank screen on the front and back side, sitting in front of a white background

Now, let's look at a step-by-step guide on how to use UIGraphicsBeginImageContextWithOptions to add a white background to your PNG image:

  1. Create a UIGraphicsBeginImageContextWithOptions call with the size of your image, a scale of 0.0, and an opacity of 1.0. This creates an image context with a white background.
  2. Use the UIRectFill method to fill the entire context with white color. This ensures that the entire image has a white background.
  3. Create a CGRect using the origin and size of your image. This rectangle will be used to draw your image onto the context.
  4. Use the CGContextDrawImage method to draw your image onto the context using the CGRect you created in step 3.
  5. Use UIGraphicsGetImageFromCurrentImageContext to retrieve the image from the current image context. This image will now have a white background.
  6. Finally, use UIGraphicsEndImageContext to clean up the graphic context and release its resources.

Here's a sample code snippet that demonstrates this process:

an iphone is shown with the front and back sides facing each other, on a white background
an iphone is shown with the front and back sides facing each other, on a white background

UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillRect(context, CGRectMake(0, 0, image.size.width, image.size.height));
CGRectdrawingRect = CGRectMake(0, 0, image.size.width, image.size.height);
CGContextDrawImage(context, drawingRect, image.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Potential Pitfalls and Workarounds

While using UIGraphicsBeginImageContextWithOptions provides a elegant solution to adding a white background to your PNG image, it's not without its pitfalls. One common issue is that the new image size might not match the original image size. This can lead to visual artifacts if you don't account for it in your app.

To avoid this, you can check the size of the new image after retrieving it using UIGraphicsGetImageFromCurrentImageContext. If the size is different, you can adjust your views or your app's layout to accommodate the new size. Alternatively, you can use the UIGraphicsBeginImageContextWithOptions method to create a context that matches the original image size, and then adjust your drawings accordingly.

IPhone 8 Apple Smartphone Search Engine Optimization PNG
IPhone 8 Apple Smartphone Search Engine Optimization PNG

In the world of iOS development, working with images can often feel like an exercise in creativity and perseverance. The UIGraphicsBeginImageContextWithOptions method is a powerful tool that can help you overcome many of the challenges you might face when working with images. By learning how to use it to add a white background to your PNG images, you've taken a significant step towards mastering iOS image manipulation.

So, the next time you find yourself fighting with a PNG image that just won't behave, remember the power of UIGraphicsBeginImageContextWithOptions. With a little bit of creativity and a lot of perseverance, you can conquer even the most daunting image challenges. Happy coding!

Best Best Free Mockups to Download
Best Best Free Mockups to Download
IPhone X
IPhone X
an iphone with a white background and no image on the screen, it's transparent
an iphone with a white background and no image on the screen, it's transparent
iphone png white background, HD Png Download
iphone png white background, HD Png Download
Mão Com Maquete De Telefone Com Tela Em Branco PNG , Telefone Com Tela Em Branco, Maquete De Telefone, Telefone PNG Imagem para download gratuito
Mão Com Maquete De Telefone Com Tela Em Branco PNG , Telefone Com Tela Em Branco, Maquete De Telefone, Telefone PNG Imagem para download gratuito
the back side of an iphone with a black frame and white screen on top of it
the back side of an iphone with a black frame and white screen on top of it
an apple iphone x with the camera facing up on a white background, viewed from the rear
an apple iphone x with the camera facing up on a white background, viewed from the rear
iphone 15 pro max white titanium
iphone 15 pro max white titanium
a white and black cell phone on a white background with no image or text to describe
a white and black cell phone on a white background with no image or text to describe