关于iphone:图片放入数组 ios开发 | 珊瑚贝

Pictures put into a array Ios developing


我的项目中有一个包含图片的文件夹,我想知道如何将这些图片从文件夹中放入数组中

我该怎么做呢?

我尝试将图像放入数组中

1
2
    UIImage*image = [[NSBundle mainBundle]pathsForResourcesOfType:@“jpg” @“jpeg” @“gif” inDirectory:@“Images”];
    NSArray*images = [[NSMutableArray alloc]initWithContentsOfFile:image];
  • 您是否在”组”或目录中有图像?如果您只是将它们拖到 xCode 中,请检查它们是否位于正确的目录中。搞乱组和文件夹真的很容易……发生在我身上……
  • 还有一些似乎大多数答案在错误时重复的东西……你不能将多个字符串传递给方法(我指的是 @”jpg” @”jpeg” @”gif” 部分)……
  • 这里的问题是多方面的。 UIImage *image 与 pathsForResourcesOfType 的设置,返回 NSString 的 NSArray。 pathsForResourcesOfType 的不正确语法(我们大多数人都忽略了,专注于其他更明显的问题)。您可以将 UIImage 或 NSArray 传递给 initWithContentsOfFile 的概念,这确实需要 NSString。即使 initWithContentsOfFile 是正确的,将它与 NSMutableArray 一起使用的概念也没有任何意义。我已经通过完全重写来回答这个问题。


您可以执行以下操作,获取文件名数组,然后用图像本身填充另一个数组(假设您正在尝试这样做)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
NSMutableArray *imagePaths = [[NSMutableArray alloc] init];
NSMutableArray *images = [[NSMutableArray alloc] init];

NSArray *imageTypes = [NSArray arrayWithObjects:@“jpg”, @“jpeg”, @“gif”, nil];

// load the imagePaths array

for (NSString *imageType in imageTypes)
{
    NSArray *imagesOfParticularType = [[NSBundle mainBundle]pathsForResourcesOfType:imageType
                                                                        inDirectory:@“Images”];
    if (imagesOfParticularType)
        [imagePaths addObjectsFromArray:imagesOfParticularType];
}

// load the images array

for (NSString *imagePath in imagePaths)
    [images addObject:[UIImage imageWithContentsOfFile:imagePath]];

顺便说一句,除非这些是很小的缩略图并且您只有很少的图像,否则通常不建议像这样一次加载所有图像。通常,由于图像会占用大量 RAM,因此您会保留文件名数组,但将图像的加载推迟到您真正需要它们时。

如果您没有成功检索图像,有两个问题:

  • 这些文件是否包含在我的捆绑包中?当您为 Target 的设置选择”构建阶段”并展开”复制捆绑资源”(见下图)时,您看到包含的图像了吗?如果您没有在此列表中看到您的图像,则它们不会在您构建应用程序时包含在应用程序中。要添加您的图像,请单击””并将它们添加到此列表中。

    enter

  • 文件是在” 组”中还是在实际的子目录中?将文件添加到项目时,您将看到以下对话框:<=”” p=”” class=”box-hide”>

    enter

    如果您选择了” 为添加的文件夹创建文件夹引用”,那么该文件夹将在您的项目中显示为蓝色(请参阅前面屏幕快照中我的”db_images”文件夹旁边的蓝色图标)。但是,如果您选择了”为添加的文件夹创建组”,则会在您的”Images”组旁边出现典型的黄色图标。最重要的是,在这种情况下,您要在子目录 “Images” 中查找图像,您希望使用 “为添加的文件夹创建文件夹引用” 选项,并在图片。

  • 最重要的是,您需要确保图像在您的应用程序包中,并且它们在您认为的位置。另请注意,iOS 区分大小写(尽管模拟器不区分),因此请确保您的 “Images” 大小写正确。

    • 当我编译代码时,他说数组是空的,但我的项目中有一个包含图像的文件夹
    • @T1992 我刚刚测试了代码,它工作正常。问题是图像是否像您认为的那样位于 “Images” 文件夹中。请参阅上面我的更新答案。
    • 你是对的,他在目录中找不到图片谢谢:)


    试试这个:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
        NSMutableArray* paths=[NSMutableArray new];
        NSFileManager* manager=[NSFileManager new];
        NSBundle* bundle= [NSBundle mainBundle];
        NSDirectoryEnumerator* enumerator= [manager enumeratorAtPath: [bundle bundlePath] ];
        for(NSString* path in enumerator)
        {
            if([path hasSuffix: @“.jpg”] || [path hasSuffix: @“.jpeg”] || [path hasSuffix: @“.gif”])
            {
                [paths addObject: path];
            }
        }

    关于解释,我建议您查看 NSDirectoryEnumerator 文档。


    阅读NSArray的initWithContentsOfFile方法的文档:

    The array representation in the file identified by aPath must contain only property list objects (NSString, NSData, NSArray, or NSDictionary objects). The objects contained by this array are immutable, even if the array is mutable.

    在您的情况下,您需要使用 NSFileManager 来枚举目录中的文件。以下是文档中目录枚举的示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    NSFileManager *localFileManager=[[NSFileManager alloc] init];
    NSDirectoryEnumerator *dirEnum =
        [localFileManager enumeratorAtPath:docsDir];

    NSString *file;
    while (file = [dirEnum nextObject]) {
        if ([[file pathExtension] isEqualToString: @“doc”]) {
            // Create an image object here and add it to
            // mutable array
        }
    }
    [localFileManager release];


    1
    [[NSBundle mainBundle]pathsForResourcesOfType:@“jpg” @“jpeg” @“gif” inDirectory:@“Images”];

    已经返回这些对象的数组。将您的行更改为:

    1
     NSArray *images = [[NSBundle mainBundle]pathsForResourcesOfType:@“jpg” @“jpeg” @“gif” inDirectory:@“Images”];


    请注意,此数组将仅保存所有图像的路径。为了制作它们的图像,您需要调用

    1
    2
    3
    4
    5
    for(NSString* imagePath in images) {

        UIImage* anImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
        //do something with your image here.
    }

    希望有帮助


    如果我正确理解了您的问题 initWithContentsOfFile 不符合您的预期,根据文档:

    “用给定路径指定的文件内容初始化一个新分配的数组。”

    另外,pathsForResourceOfType 已经在创建一个数组,而不是 UIImage,你可以简单地这样做:

    1
    NSArray* images = [[NSBundle mainBundle]pathsForResourcesOfType:@“jpg” @“jpeg” @“gif” inDirectory:@“Images”];

    来源:https://www.codenong.com/13804337/

    微信公众号
    手机浏览(小程序)

    Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

    Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

    Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8959.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
    0
    分享到:
    没有账号? 忘记密码?