如何用.jpg为所有字符串写NSString?

时间:2014-06-29 13:27:28

标签: xcode nsstring

我是编程新手,我想知道如何编写带有.jpg扩展名的NSString。

我有代码,我的ViewController有一些标题和我用于NSString的标题。比我使用此字符串查找具有相同名称的图像。但我不知道配置.jpg扩展名。

这是我的代码:

- (void)viewDidLoad{

    [super viewDidLoad];
    NSString*imageName = [NSString stringWithFormat:(@"% .jpg", self.title)];
    UIImage*image = [UIImage imageNamed:imageName];
    [imageView setImage:image];
   NSLog(@"%", imageName);
}

有趣的是,我的NSLog系列不起作用。可能是我的prepareForSegue方法中的问题?:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
     if ([segue.identifier isEqualToString:@"Cell Segue"]) {
     ImageShowingViewController*ivc = segue.destinationViewController;
     ivc.title = [self.objects objectAtIndex:(self.tableView.indexPathForSelectedRow.row)];

  }
}

当我在TableViewController中推送一些Cell时,推送segue被激活并显示我的第二个ViewController。这部分代码工作正常,但即使我的viewDidLoad完成,也没有NSLog行。但是当我在启动viewDidLoad方法上查看Breakpoint并在NSLog行上显示它时  " imageName __NSCFString * @"面包" 0x00000001095515f0

任何帮助表示感谢。

问题解决了。应该是[NSString stringWithFormat:@"%@",self.title]而不是[NSString stringWithFormat:(@"%@",self.title)]

2 个答案:

答案 0 :(得分:1)

使用

NSString *imageName = [NSString stringWithFormat:@"%@.jpg", self.title];
UIImage*image = [UIImage imageNamed:imageName];
[_image setImage:image];
应该使用

stringWithFormat:代替stringWithString:。此外,不应在表达式中使用括号。

此外,还不清楚[image setImage:image];应该做什么。第一个image很可能是图像视图?我已经更新了我的答案,因此它与加载的图像不同,但您需要确定该值应该是什么。

答案 1 :(得分:0)

缺少@

- (void)viewDidLoad{

    [super viewDidLoad];
    NSString*imageName = [NSString stringWithString:(@"%@.jpg", self.title)];
    UIImage*image = [UIImage imageNamed:imageName];

    //[yourImageView setImage:image]; then set your UIImageViews image with this
}