1

バンドルから xib とイメージにアクセスしようとしているフレームワークがあります。xib には問題なくアクセスできますが、UIImageView に画像を読み込めません。ファイルが存在し、コンソール出力が「はい」であるかどうかも確認します...

画像を読み込む方法はありますか?

フレームワークのviewcontrollerファイルにxibをロードするコード

self = [super initWithNibName:@"ViewController_iPad" bundle:[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"MyBundle" ofType:@"bundle"]]];

画像を読み込むコード

- (void) loadImage
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"MyBundle.bundle/test" ofType:@"png"];
    BOOL present = NO;
    present = [[NSFileManager defaultManager] fileExistsAtPath:path];
    if (present)
    {
        NSLog(@"MyBundle.bundle/test exists");
    }
    else
    {
        NSLog(@"MyBundle.bundle/test does not exists");

    }

    self.testImageView.image = [UIImage imageNamed:path];

    self.testImageView2.image = [UIImage imageWithContentsOfFile:path];
}

MyTestHarnessApp[11671:c07] MyBundle.bundle/test が存在します

4

5 に答える 5

2

パスが正しく解決されない場合+[UIImage imageNamed:]は、常に次を使用できます。

self.testImageView.image = [UIImage imageWithContentsOfFile: path];
于 2013-05-22T15:46:07.623 に答える
1

次のものを使用できます。

self.testImageView.image = [UIImage imageNamed:@"MyBundle.bundle/test.png"];

ただし、XIB もバンドルに入れると、インターフェイスはバンドルに相対的になるようです。

self.testImageView.image = [UIImage imageNamed:@"test.png"]
于 2013-05-22T15:23:34.197 に答える
1

一歩一歩進んでみてください。

まず、メイン バンドル内のバンドルへのパスを取得します。

NSString *myBundlePath=[[NSBundle mainBundle] pathForResource:MyBundle ofType:@"bundle"];

バンドルに何かがあるかどうかを確認します。これは、二重チェックのためのものです。機能するようになったら、このコードを省略できます。しかし、物事を再確認することは常に良い考えです。

 NSBundle *imageBundle=[NSBundle bundleWithPath: myBundlePath];
 NSArray *allImageURLs=[imageBundle URLsForResourcesWithExtension:@"png" subdirectory:nil];
 NSLog(@"imageURLS:  %@",allImageURLS);

既にバンドル パスがあるため、イメージ名をパスに追加します。または、上記のコードに既にある URL のリストから取得することもできます。

NSString *myImagePath=[myBundlePath stringByAppendingPathComponent:@"test.png"];
NSLog(@"myImagePath=%@",myImagePath);

次に、フルパスがあるので、画像をロードします

UIImage *testImage = [UIImage imageWithContentsOfFile:backgroundImagePath];

配送アプリから取得したコード。

幸運を

ティム

于 2013-05-22T18:23:01.193 に答える