2

NSArray画像付きです。画像名はA、B、C、Dです。NSLog配列からこれらの画像の名前を付ける必要があります

NSLog(@"Name == %@", [Array objectAtIndex:1]);

これの代わりに何を使用する必要がありますか?

4

6 に答える 6

9

AUIImageはそのファイル名を保存しません。それらが作成されたファイルの名前を追跡したい場合は、それらも保存する必要があります。

于 2012-07-18T09:55:35.390 に答える
2

ワトソンが示唆しているように、それは不可能です。このためには、imageNameを保存できる別の配列を取得する必要があります。それ以外の場合は、imageNameをキーとしてNSMutableDictionary保存し、 Arrayオブジェクトをそのオブジェクトとして保存して後で読み取ることができます。

于 2012-07-18T10:01:53.230 に答える
2

私の知る限り、UIImageオブジェクトから画像ファイルの名前を取得することはできません。これを実行したい場合は、名前を画像と一緒にNSDictionaryオブジェクトに保存できます。

NSArray * imageNames = [NSArray arrayWithObjects:@"A.png", @"B.png", @"C.png", nil];
NSMutableArray * array = [NSMutableArray arrayWithCapacity:[imageNames count]];
for (NSString * imageName in imageNames)
  [array addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                    [UIImage imageNamed:imageName], @"image", imageName, @"name", nil]];

次に、次のようにログに記録できます。

NSLog(@"name = %@", [[array objectAtIndex:1] valueForKey:@"name"]);
于 2012-07-18T10:05:11.413 に答える
2

配列の「description」メソッドを連結された画像の名前でオーバーライドすると、正常に機能する可能性があります。

この場合、NSLogは、各オブジェクトにそれ自体を説明する文字列を要求し、オブジェクトに-descriptionメソッドを送信することで機能します。(注:オブジェクトがdescriptionメソッドをオーバーライドしない場合、NSObjectから継承された-description実装を取得します。これは、次のようになる傾向があります。UsingTheDescriptionMethodを参照してください。

:descriptionメソッドは、デバッグ目的でのみ使用する必要があります

よろしく。

于 2012-07-18T10:30:49.253 に答える
1

それはいけません。A、B、C、Dがインスタンス変数である場合、それらはあなたが制御できる唯一のものです。

if ((UIImage*)[Array Objectatindex:1] == A) 
   bla-bla-bla you know that it's "A"

割り当てられると、画像名に到達できません。

于 2012-07-18T09:56:41.930 に答える
0

NSLog全体NSArrayを使用したい場合は、これを使用してください。

NSArray *_array = // your array
NSLog(@"array == %@", _array);

更新:#1

使用可能な方法の1つは次のとおりです。オブジェクトのみで埋めるのNSMutableArrayではなく、次のような内容でUIImage「NSDictionary」オブジェクトをに追加する必要があります。NSMutableArray

NSMutableArray *_array = [NSMutableArray array];

// you could put this part inside a loop if you like
NSString *_imagePathWithName = @"...";
UIImage *_newImage = [UIImage imageNamed:_imagePathWithName]; // when you load it from you application bundle
// or [UIImage imageWithContentsOfFile:_imagePathWithName]; // loading from other place
NSDictionary *_imageDictionary = [NSDictionary dictionaryWithObjectsAndKeys:_newImage, @"keyForUIImage", _imagePathWithName, @"keyForFullPathAndName", nil];
[_array addObject:_imageDictionary];

配列から名前を読み取りたい場合

for (NSDisctionary *_dictionary in _array) {
    UIImage *_image = (UIImage *)[_dictionary valueForKey:@"keyForUIImage"];
    NSString *_fullPathWithName = (NSString *)[_dictionary valueForKey:@"keyForFullPathAndName"];
    // do whatever you'd like with the images and the path
}
于 2012-07-18T09:56:29.353 に答える