0

これは「これには簡単な答えがあると確信している」という質問のもう1つですが、私は困惑しています。

テンプレートの分割ビューコントローラを使用しています。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;
}

しかし、ここに奇妙なことがあります...変数(この場合は「what」)を文字列リテラルに置き換えると、次のようになります。

NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:[NSString 
                 stringWithFormat:@"strips/%@", @"grumbles300.jpg"]];

...それは機能し、その1つの画像を表示します。でもそこで変数が使えるようになりたい!!

オビワンケノービを助けて!あなたは私の唯一の希望です。

4

1 に答える 1

0

困惑するオペレーターエラー。

バンドルにバンチャ画像を追加していましたが、ファイル名に追加する必要がある、それらを含むフォルダーを追跡しているようです。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;
}
于 2012-05-06T18:49:36.507 に答える