0
votes

I am new in programming and i would like to know how i can write NSString with .jpg extension.

I have code where my ViewController has some title and that title i use for NSString. Than i use this string to find image with same name. But i dont know to configure that .jpg extension.

Here is my code:

- (void)viewDidLoad{

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

Funny is that my NSLog line don't work. Can be the problem in my prepareForSegue method?:

-(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)];

  }
}

When i push some Cell in my TableViewController push segue is activated and show my second ViewController. This part of code works fine but no NSLog line there even if my viewDidLoad was finished. But when i look on Breakpoint on start viewDidLoad method and on NSLog line it show me
"imageName __NSCFString * @"bread" 0x00000001095515f0

Any help appreciated.

Problem solved. It should be [NSString stringWithFormat:@"%@", self.title] and not [NSString stringWithFormat:(@"%@", self.title)]

2
Your NSLog has the same problem as before, it should be NSLog(@"%@", imageName);.ThomasW
Yes i have figured out. Many thanks for your help!M.Koptak

2 Answers

1
votes

Use

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

stringWithFormat: should be used instead of stringWithString:. Also, parenthesis shouldn't be used in the expression.

Also, it is not clear what [image setImage:image]; is supposed to do. Most likely the first image is an image view? I've updated my answer so it is distinct from the loaded image, but you'll need to determine what the value should be.

0
votes

Missing the @

- (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
}