当我尝试使用图像(特定图像或来自数组的图像)加载UIImageView时,我的UIImageView只显示运行应用程序的空白;根本没有图像。我没有收到任何语法错误。在下面你会看到我尝试了两个不同的任务,一个是直接的,另一个是使用数组,但都没有工作。 NSLog确认我的imageDisplay变量中存储了一个数字。有任何想法吗?我错过了什么?
- (void)viewDidLoad
{
KanaCharacters = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"a.png"],
[UIImage imageNamed:@"i.png"],
[UIImage imageNamed:@"u.png"],
[UIImage imageNamed:@"e.png"],
[UIImage imageNamed:@"o.png"],
[UIImage imageNamed:@"ka.png"],
[UIImage imageNamed:@"ki.png"],
[UIImage imageNamed:@"ku.png"],
[UIImage imageNamed:@"ke.png"],
[UIImage imageNamed:@"ko.png"],
nil];
int imageDisplay = arc4random_uniform([KanaCharacters count]);
NSLog(@"%d", imageDisplay); //3
Monster1.image = [[UIImage alloc] initWithCGImage:(__bridge CGImageRef)([UIImage
imageWithContentsOfFile:@"e.png"])];
// Monster1 = [[[UIImageView alloc]initWithFrame:(CGRectMake(49, 49, 45, 45))]
initWithImage:[KanaCharacters objectAtIndex:imageDisplay]];
答案 0 :(得分:2)
如果您希望Monster1
图像来自数组kanaCharacters
且索引编号为imageDisplay
,那么您不会这样做吗?
NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c", nil]; //Setup array
int imageDisplay = arc4random_uniform([array count]); //Find random int from array count
UIImageView *monster1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)]; //Setup UIImageView
monster1.image = [UIImage imageNamed:array[imageDisplay]]; //Display image which is randomly selected from array
[self.view addSubview:monster1]; //Add image to view
答案 1 :(得分:1)
不能相信这很简单,但现在是:
[Monster1 setImage:[KanaCharacters objectAtIndex:imageDisplay]];
所需要的只是括号。