2012-04-11 6 views
0

からイメージをロードする方法を、私は現在、このメソッドを介して私のUIImageViewで画像を保存しています:だから、NSHomeDirectory

- (void)applicationDidEnterBackground:(UIApplication*)application { 

    NSString *image1 = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image1.png"]; 
    NSString *image2 = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image2.png"]; 
} 

画像がうまく保存するが、私はのviewDidLoadでUIImageViewsの画像を設定する方法が分かりません。これは私が試したことです:

- (void)viewDidLoad 
{ 
    self.imageView.image =[UIImage imageNamed:[(NSHomeDirectory *)Documents objectForKey:@"image1"]]; 

    self.imageView2.image =[UIImage imageNamed:[(NSHomeDirectory *)Documents objectForKey:@"image1"]]; 
} 

しかし、明らかにそれは動作していません。私はここで基礎を理解するのに困っている。どんな助けも素晴らしいだろう、ありがとう!

答えて

3

-imageNamed:メソッドは、名前付きイメージがまだキャッシュされていない場合、アプリケーションのメインバンドルを調べます。ドキュメントから:

ファイルの名前。イメージが初めてロードされる場合、メソッドはアプリケーションのメインバンドル内で指定された名前のイメージを探します。

あなたが代わりに-imageWithContentsOfFile:をしたい

UIImage class referenceを参照してください)。

+0

ありがとうございました。したがって、私のviewDidLoadは次のようになります: 'NSString * image1String = [[NSBundle mainBundle] pathForResource:@" Documents/image1 "ofType:@" png "]; ' ' self.imageView = [UIImage imageWithContentsOfFile:image1String]; '?私は、警告 "UIImageからUIImageView *を割り当てる互換性のないポインタ型"を取得しています。 – John

+0

あなたは近くです。 '-imageWithContentsOfFile:'でロードした 'UIImage'から' UIImageView'オブジェクトを作成する必要があります。したがって: 'self.imageView = [[[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:imagePath]] autorelease];' –

+0

ありがとうございます! – John