You will need OpenAI API key and you need to verify your organization in order to get access to the new GPT-Image-1 model. This is a new requirement from OpenAI since this is a protected model.


See All Source Codes

For 10% OFF - use coupon code ZWV-YJN-TVZ




1) Download Sample Project
Download the sample project that simply shows an image, text input for the prompt, and submit button.


2) Create URLRequest

Open DataManager file and look for generateImage function. In this function we will start with creating the URLRequest.


Line 4 uses the 300 seconds and Line 7 uses the OpenAPI key from AppConfig.
Make sure to replace the API key in AppConfig with your own key.



3) Add Image Data

In the same function, we must add the image data. For this demo, we are using the "sample-image" that comes with the project. You can implement a photo picker, and let users import their own images.


Line 6 is a simple utility function to append string data.
Line 11 is where we are using the sample image, and appending its data to the body.



4) OpenAI Model & Prompt

Next step is to add the OpenAI model name and the prompt to the body.


Line 7 sets the OpenAI model from AppConfig, which is gpt-image-1.
Line 11 is where we set whatever prompt the user typed.
On Line 14 we are setting the httpBody to our data.



5) Start API Request

We are almost done. In this step, we put everything together and start the API request.


Starting on Line 10 we are parsing the JSON response.
The image comes back as base64 string, that's why on Line 14 we convert that string to data.
Then on Line 16 we create the image from data.



6) Display Image Result

Great job! This is the final step. You just need to update the SwiftUI view to execute this API.
You already have this class in the project, simply add the Lines 18, 19, 20 and 22.


Line 18-20 will show a loading view while the image is processing.
Line 22 will call the generateImage() function that you just finished updating.



Conclusion
You've successfully integrated the GPT-Image-1 API into a SwiftUI app.
From here, you can add new features, create a better user interface or even try style transfer.
If you've made it this far, use GPT-IMAGE-1 discount code for our AI Character full source code.




FAQ:


Q) Can I generate images without providing my own image?
A) Yes, you can generate images from prompt only. Upadte the API in AppConfig to https://api.openai.com/v1/images/generations and skip Lines 10-17 in Step #3 of the tutorial.


Q) What API is AI Character source code using?
A) The source code we offer for sale uses the same GPT-Image-1 model, and the editing API covered in this tutorial (https://api.openai.com/v1/images/edits).





If you have any questions or suggestions, please feel free to send an email to support@apps4world.com
Thanks for reading!