0

我想从目录中显示图像。在目录中,我创建了文件夹来存储图片。我收到一条错误消息,提示使用未声明的标识符“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;
}
4

1 回答 1

0

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

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

在这里,您将 anNSArray转换为 a NSMutableArray,但它仍然是 a 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;
}
于 2013-08-30T08:38:27.860 回答