-1

I'm using the HSImageSidebarView and in the example included in the open source project, this is how to load the images in the sidebar :

-(UIImage *)sidebar:(HSImageSidebarView *)sidebar imageForIndex:(NSUInteger)anIndex {
    int color = [[colors objectAtIndex:anIndex] intValue];
    switch (color % 3) {
        case 0:
            return [UIImage imageNamed:@"Blue"];
            break;
        case 1:
            return [UIImage imageNamed:@"Red"];
            break;
        default:
            return [UIImage imageNamed:@"Green"];

    }
}

What my problem is how to add my own images from the NSDocumentDirectory. Here is my array :

self.images = [NSMutableArray new];  
for(int i = 0; i <= 100; i++) 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];

    NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", i]]; 
    NSLog(@"savedImagePath=%@",savedImagePath);

    if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
        [images addObject:[UIImage imageWithContentsOfFile:savedImagePath]]; 
        NSLog(@"file exists");
    } 
    NSLog(@"file does not exist");
} 

Thanks for the help!

4

1 に答える 1

1

このようなことをします。

-(UIImage *)sidebar:(HSImageSidebarView *)sidebar imageForIndex:(NSUInteger)anIndex {

    return (UIImage*)[self.images objectAtIndex:anIndex];

}

を既に配列に格納UIImageしています。したがって、例のようなメソッドimagesを使用する必要はありません。-imageNamed:代わりimagesに、メソッドを使用して配列から上記のインデックスにある画像を返しますobjectAtIndex:

お役に立てれば。

于 2012-06-14T04:08:14.640 に答える