Jim Botts
Jim Botts

Reputation: 59

Programmatically placing an image in an UIImageView on a UIViewController

I have an image (a .png file) that I want to place in an ImageView in a ViewController. I use the following code but the simulator gives me a blank white view without the image. The .png file is in the same directory as the ViewController files. Here is the code:

@implementation ViewController
{
    NSArray *_pArray;
    UIImage *_image;
    UIImageView *_imageView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _image = [[UIImage alloc] initWithContentsOfFile:@"TM-1P2.png"];
    _imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    [_imageView setImage:_image];
    [_imageView setContentMode:UIViewContentModeScaleAspectFit];
    [self.view addSubview:_imageView];    
}

Upvotes: 1

Views: 2132

Answers (4)

Mohamad Chami
Mohamad Chami

Reputation: 1234

- (void)viewDidLoad {

[super viewDidLoad];

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.layer.frame.size.width,   self.view.layer.frame.size.height)];

imgView.image = [UIImage imageNamed:@"emptyCart.jpeg"];

imgView.backgroundColor = [UIColor whiteColor];

imgView.contentMode = UIViewContentModeCenter;

[self.view addSubview: imgView];

}

Upvotes: 0

Rob
Rob

Reputation: 437392

If you examine at _image (either NSLog or in the debugger), it probably is nil. With initWithContentsOfFile you should specify the entire path, for example:

NSString *path = [[NSBundle mainBundle] pathForResource:@"TM-1P2" ofType:@"png"];
_image = [[UIImage alloc] initWithContentsOfFile:path];

Alternatively, you can use the following, which automatically looks for the image in the bundle:

_image = [UIImage imageNamed:@"TM-1P2.png"];

This latter syntax, imageNamed, caches the image (i.e. will keep it in memory even if you dismiss the view controller). That's great if you have to use the same image again and again throughout the app (because it won't have to reload it every time), but if you only use it once, you might not want to use imageNamed. As the imageNamed documentation says:

If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.

Note, both of these assume that you've successfully added this image to your bundle.

If, on the other hand, the image is in your Documents folder, you could load it like so:

NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *path = [documentsPath stringByAppendingPathComponent:@"TM-1P2.png"];
_image = [[UIImage alloc] initWithContentsOfFile:path];

Finally, note that the iOS devices are case sensitive (generally the simulator is not), so make sure you have your capitalization correct.


Unrelated to your question, those variables in between the braces probably should not be defined in the @implementation, but rather you should put them in a @interface. For example, you could put them in your .h file, or better, you can put them in a private class extension in your .m file, right before the @implementation:

@interface ViewController ()
{
    NSArray *_pArray;
    UIImage *_image;
    UIImageView *_imageView;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"TM-1P2" ofType:@"png"];
    _image = [[UIImage alloc] initWithContentsOfFile:path];
    _imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    [_imageView setImage:_image];
    [_imageView setContentMode:UIViewContentModeScaleAspectFit];
    [self.view addSubview:_imageView];    
}

// ...

@end

Upvotes: 4

Rajiv
Rajiv

Reputation: 31

If you have your image named as "TM-1P2.png" in your bundle, you can simply do the following:

_image = [UIImage imageNamed:@"TM-1P2.png"];

Upvotes: 0

user189804
user189804

Reputation:

Have you stepped through in the debugger?

I would be interested to see if _image is non-nil - and the most likely reason for it being nil is that you have not added it to your project.

Upvotes: 0

Related Questions