【问题标题】:loading images from documents directory into an array将文档目录中的图像加载到数组中
【发布时间】:2012-03-31 16:57:25
【问题描述】:

我有一些 jpg 图像,我从相机照片中保存到文档目录中。我想在文档目录中找到所有 jpg 图像并将它们加载到一个数组中,以便它们可以在溢出视图中。

这是我在文档目录中查找和排序 jpg 图像的代码。

   NSString *extension = @"jpg";
        NSFileManager *fileManager = [NSFileManager defaultManager];



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

        NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];  
        NSMutableArray *jpegFiles = [NSMutableArray arrayWithCapacity: [contents count]];

       NSString *filename;
        for (filename in contents)

        {
            if ([[filename pathExtension] isEqualToString:extension]) 
                     {
             [jpegFiles addObject:[UIImage imageNamed:[documentsDirectory stringByAppendingPathComponent:filename]]];


            }
        }

    // Add the coverflow view
    coverflow = [[TKCoverflowView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    coverflow.coverflowDelegate = self;
    coverflow.dataSource = self;
    [self.view addSubview:coverflow];
    [coverflow setNumberOfCovers:6];

- (void) coverflowView:(TKCoverflowView*)coverflowView coverAtIndexWasBroughtToFront:(int)index{
        NSLog(@"Front %d",index);
}

- (TKCoverflowCoverView*) coverflowView:(TKCoverflowView*)coverflowView coverAtIndex:(int)index{


    TKCoverflowCoverView *cover = [coverflowView dequeueReusableCoverView];

    if(cover == nil){
        // Change the covers size here
        cover = [[TKCoverflowCoverView alloc] initWithFrame:CGRectMake(0, 0, 280, 210)]; // 224
        cover.baseline = 200;

    }
    cover.image = [covers objectAtIndex:index % [covers count]];

    return cover;

}

我已经尝试了我能想到的一切,但没有任何效果。

有人建议我通过从路径名数组中获取图像的路径来加载每个图像,并使用方法 imageWithContentsOfFile 来加载图像。

我已经查看了这个并搜索了如何做到这一点,但没有运气。有人可以告诉我如何做到这一点。

【问题讨论】:

    标签: nsmutablearray coverflow nsdocumentdirectory


    【解决方案1】:

    你应该这样做,

    if ([[filename pathExtension] isEqualToString:extension]) 
    {
         [jpegFiles addObject:[UIImage imageWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:filename]]];
    }
    

    【讨论】:

    • 代码通过{if([[filename pathExtension]isEqualToString:extension])就可以了
    • 对不起,这里的其他编辑花了很长时间代码通过 {if([[filename pathExtension]isEqualToString:extension]) {[jpegFiles addObject: filename];}} 它创建一个数组扩展名为 .jpg 的所有图像文件的文件名。我尝试使用您的代码,但没有成功。我想做的只是将jpegFiles数组中的文件加载到covers数组中并使用它们,但真的不知道如何尝试arrayWithArray,arrayWithArray Copy只是不断崩溃。非常感谢您的帮助和意见。
    • 我给出的代码为您提供了 jpegFiles 数组中您的文档目录中所有 jpeg 文件的 UIImage 对象。
    • 所以我修改了我的代码以反映您的建议,以及相关页面其余部分的代码。当我运行程序时它崩溃说 '-[NSArrayM insertObject:atIndex:] 对象不能为 nil'* 也许我做错了什么。你能指出我的错误吗?
    • 糟糕。一个小修正!而不是 [UIImage imageNamed:path];使用 [UIImage imageWithContentsOfFile:path];
    猜你喜欢
    • 1970-01-01
    • 2015-01-07
    • 2015-08-08
    • 2012-09-04
    • 1970-01-01
    • 2013-07-16
    • 2010-11-04
    • 2012-01-27
    • 2015-02-12
    相关资源
    最近更新 更多