这是另一个“我确信这个问题很容易回答”,但我发现自己感到困惑。
我正在使用模板中的拆分视图控制器。我成功地将NSString传递给_detailItem变量,但是无法使用它来创建并将图像加载到UIImageView(名为“stripImage”)中。
直到: [self.stripImage setImage:[UIImage imageNamed:_detailItem]];
如果我把一个字符串文字放在同一行代码中(比如@“image20.jpg”),它就可以了。
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
NSLog(@"_detailItem is still: %@",_detailItem);
// correctly reports the name of the imagefile (for example: "image20.jpg"
[self.stripImage setImage:[UIImage imageNamed: _detailItem]];
NSLog(@"The image being shown is: %@",self.stripImage.image);
//returns "The image being shown is: (null)"
}
}
请帮助我防止爆炸。提前谢谢。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Selected row %i",[indexPath row]);
NSString *imageName = [[stories objectAtIndex:[indexPath row]] objectForKey:@"link"];
NSLog(@"The image is: %@", imageName);
// These two work, oddly enough...
self.detailViewController.detailTitle.text = [[stories objectAtIndex:[indexPath row]]
objectForKey:@"title"];
self.detailViewController.detailSubtitle.text = [[stories objectAtIndex:[indexPath row]]
objectForKey:@"subtitle"];
// this one, not so much...
[self.detailViewController loadUpImageWith:imageName];
}
- (void)loadUpImageWith:(NSString *)what
{
NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:[NSString
stringWithFormat:@"strips/%@", what]];
NSLog(@"The file's url is: %@",path);
UIImage *img = [UIImage imageWithContentsOfFile:path];
NSLog(@"The resultant image is: %@",img);
stripImage.image = img;
}
NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:[NSString
stringWithFormat:@"strips/%@", @"grumbles300.jpg"]];
...它有效,显示一个图像。但我希望能够在那里使用变量!!
帮助我Obiwan Kenobi!你是我唯一的希望。
答案 0 :(得分:0)
我在包中添加了buncha图像,但它显然跟踪了包含它们的文件夹,需要添加到文件名中。 I想想UIImage imageNamed:会跟踪图像,只要它们包含在主要包中。显然不是。
对于那些经历过相同脑屁的人来说,这可能是有用的。
解决这个问题后,很容易做到这一点:
- (void)loadupDetailTitle:(NSString *)title
subtitle:(NSString *)subtitle
image:(NSString *)imageName
{
NSString *path = [@"strips/" stringByAppendingPathComponent:imageName];
stripImage.image = [UIImage imageNamed:path];
detailTitle.text = title;
detailSubtitle.text = subtitle;
}