How to programmatically set images in iOS

Опубликовано: 14 Февраль 2026
на канале: techytube
4,960
3

Subscribe to our channel here for notifications on new video trainings. For more videos on technology, visit our website at http://www.techytube.com.
By pavel@techytube, for more from this author visit:
http://www.techytube.com/author/pavel

The UIImageView class provides a view-based container for displaying images on iOS devices screens.
Most of the times we will set images for our UI controls visually, using the Xcode integrated Interface Builder.
But there may as well situations when we need to do that programmatically (probably we need to dynamically set images depending on different flags or situations). In those cases we first have to created the object that will contain the image:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
Than we must set the actual image, by using the "image" property in case of UIImageView objects:
imageView.image = [UIImage imageNamed:@"testImage"];
And finally add that UI object to the main view:
[self.view addSubview:imageView];
iOS will take care of all the details, and will correctly load the corresponding image for the specific device.
Note that is now necessary to specify the .png or some other extension for the image. However, you can do that if you want, and you'll get the same results.

For more details, please visit Apple's Online Documentation at:

http://developer.apple.com/library/io...