【问题标题】:iOS trouble loading an image file to view from view controller programmaticallyiOS 无法以编程方式加载图像文件以从视图控制器查看
【发布时间】:2013-12-23 14:19:13
【问题描述】:

我有一个带有属性的视图,它应该从我的视图控制器中获取一个 NSString 并显示一个图像。如果我在视图中使用“320x213-1.png”对 locationImageFile 进行硬编码,则图像会正确显示,但如果我尝试从视图控制器设置 locationImageFile,则图像不会出现。我觉得我错过了一些非常基本的东西。旁注:我似乎可以从我的视图控制器中设置 locationTitle 属性而没有任何问题。

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

【问题讨论】:

  • 这个问题发生在你没有硬编码的时候,告诉我们这是如何工作的,我怀疑在它传递给函数之前这个值是 nil,你检查了吗?
  • 正常运行的硬编码行:UIImage *locationImage = [UIImage imageNamed:@"320x213-1.png"]; NSLog 确实显示该值为 nil。

标签: ios objective-c model-view-controller uiimageview uiimage


【解决方案1】:

我认为这是因为loadView 方法的调用早于viewDidLoad 方法。

所以当你设置_locationView.locationImageFile = @"320x213-1.png";时,容器视图的图像没有被设置。

你可以考虑将locationImageContainer声明为locationView的一个属性,直接设置为image.like

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

而不是将 locationImageFile 设置为属性。

【讨论】:

    【解决方案2】:

    LocationView被初始化时,实例变量locationImageFile的值等于nil。像这样覆盖LocationView.m 内的locationImageFile 属性的设置器:

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

    【讨论】:

      【解决方案3】:

      你可以试试,这样做:

      _locationView.locationImageFile = [UIImage imageNamed:locationImageFile];

      如果你这样做: _locationView.locationImageFile = locationImageFile;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-15
        • 1970-01-01
        • 2011-05-09
        • 2020-05-01
        • 2019-06-18
        • 2023-03-16
        • 2016-05-05
        相关资源
        最近更新 更多