使用iCarousel显示目录中的图像

时间:2013-08-30 08:26:08

标签: ios directory icarousel

我想要从目录中显示图像。在目录中,我已经创建了用于存储图片的文件夹。我收到的错误是使用未声明的标识符'NSFileManagerdefaultManager'和使用未声明的标识符'documentsDirectory';你是说'NSDocumentDirectory'吗?和坏接收器类型'NSUInteger'(又名'unsigned int')。我无法摆脱这一点。我正在尝试,但它只是不起作用。

-(void)viewDidLoad
{
    [super viewDidLoad];

    //configure carousel
    imageArray1 = (NSMutableArray *)[[NSFileManager defaultManager] directoryContentsAtPath: fPath];
    NSString *location=@"Tops";
    NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location];
    NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
    imageArray1 = [directoryContent mutableCopy];

    imageArray2 = [[NSMutableArray alloc] init];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    location=@"Bottoms";
    fPath = [documentsDirectory stringByAppendingPathComponent:location];

    directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
    imageArray2 = [directoryContent mutableCopy];

    carousel1.type = iCarouselTypeLinear;
    carousel2.type = iCarouselTypeLinear;
}

1 个答案:

答案 0 :(得分:0)

您的代码中有一些非常奇怪的东西:

imageArray1 = (NSMutableArray *)[[NSFileManager defaultManager] directoryContentsAtPath: fPath];

在这里,您要向NSArray投射NSMutableArray,但它仍然是NSArray。你不能这样做。

NSString *location=@"Tops";
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location];
NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
imageArray1 = [directoryContent mutableCopy];

然后构建fpath字符串,您已经在第一行使用了该字符串。您正在进行正确转换为NSMutableArray

你只需要删除第一行,它不是必需的,什么都不做。这条线也是如此:

imageArray2 = [[NSMutableArray alloc] init];

您只需创建一个空数组,然后再分配另一个数组:

imageArray2 = [directoryContent mutableCopy];

只需删除imageArray2 = [[NSMutableArray alloc] init];即可,只会使用不必要的内存。

这会更好用:

-(void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSFileManager *fileManager  = [NSFileManager defaultManager];

    //configure carousel
    NSString *fPath = [documentsDirectory stringByAppendingPathComponent:@"Tops"];
    NSArray *directoryContent = [fileManager directoryContentsAtPath: fPath];
    imageArray1 = [directoryContent mutableCopy];

    //configure carouse2
    fPath = [documentsDirectory stringByAppendingPathComponent:@"Bottoms";];
    directoryContent = [fileManager directoryContentsAtPath:fPath];
    imageArray2 = [directoryContent mutableCopy];

    carousel1.type = iCarouselTypeLinear;
    carousel2.type = iCarouselTypeLinear;
}