简体   繁体   中英

iOS trouble loading an image file to view from view controller programmatically

I have a view with a property that should take an NSString from my view controller and display an image. If I hard code locationImageFile with "320x213-1.png" in my view, the image displays properly, but if I try to set locationImageFile from my view controller, the image doesn't appear. I feel like I'm missing something pretty basic. Side note: I seem to be able to set the locationTitle property from my view controller without any problems.

LocationViewController.m

#import "LocationViewController.h"
#import "LocationView.h"

@implementation LocationViewController
{
    LocationView *_locationView;
}

- (void)loadView
{
    CGRect frame = [[UIScreen mainScreen] bounds];
    _locationView = [[LocationView alloc] initWithFrame:frame];
    [self setView:_locationView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view.

    _locationView.locationTitle.text = aLocation.name;
    _locationView.locationImageFile = @"320x213-1.png";

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

LocationView.m

#import "LocationView.h"

@implementation LocationView

@synthesize locationTitle;
@synthesize locationImageFile;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        [self setBackgroundColor: [UIColor blueColor]];

        // Image container
        UIImage *locationImage = [UIImage imageNamed:locationImageFile];
        UIImageView *locationImageContainer = [[UIImageView alloc] initWithImage:locationImage];
        [locationImageContainer setTranslatesAutoresizingMaskIntoConstraints:NO];
        locationImageContainer.backgroundColor = [UIColor yellowColor];
        [self addSubview:locationImageContainer];

        // Text line
        locationTitle = [[UILabel alloc]init];
        [locationTitle setTranslatesAutoresizingMaskIntoConstraints:NO];
        locationTitle.backgroundColor = [UIColor whiteColor];
        [self addSubview:locationTitle];

        }
    return self;
}

@end

When LocationView is initialized, the value of the instance variable locationImageFile equals nil . Override the setter of the locationImageFile property inside LocationView.m like this:

- (void)setLocationImageFile:(NSString *)aLocationImageFile
{
    locationImageFile = aLocationImageFile;
    locationImageContainer.image = [UIImage imageNamed:aLocationImageFile];
}

I think this is because the loadView method are invoke earlier than viewDidLoad method.

so when you set _locationView.locationImageFile = @"320x213-1.png"; ,the containerView's image are not being set.

You can consider declare locationImageContainer as a property of locationView , and set it's image directly.like

_locationView.locationImageContainer.image = [UIImage imageNamed:@"320x213-1.png"];

instead of setting locationImageFile as a property.

You can try, doing it like this:

_locationView.locationImageFile = [UIImage imageNamed:locationImageFile];

if you did it like this: _locationView.locationImageFile = locationImageFile;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM