gpt4 book ai didi

iOS 无法加载图像文件以编程方式从 View Controller 查看

转载 作者:行者123 更新时间:2023-11-28 22:19:44 27 4
gpt4 key购买 nike

我有一个 View ,其属性应该从我的 View Controller 获取 NSString 并显示图像。如果我在我的 View 中使用“320x213-1.png”对 locationImageFile 进行硬编码,则图像会正确显示,但如果我尝试从我的 View Controller 设置 locationImageFile,则图像不会出现。我觉得我缺少一些非常基本的东西。旁注:我似乎可以毫无问题地从我的 View Controller 设置 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

最佳答案

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

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

你可以考虑将 locationImageContainer 声明为 locationView 的属性,并直接设置它的图像。就像

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

而不是将 locationImageFile 设置为属性。

关于iOS 无法加载图像文件以编程方式从 View Controller 查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20745410/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com