Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是否将URL列表添加到数组?_Ios_Objective C - Fatal编程技术网

Ios 是否将URL列表添加到数组?

Ios 是否将URL列表添加到数组?,ios,objective-c,Ios,Objective C,我正在使用创建存储在我的网站上的图像库。当前添加图像的方法(如Github上的示例所示)是 但我想添加数百张图片,而这并不是最理想的方式。对我来说,哪种方法更容易实现这一点 谢谢 您必须从URL列表开始。我要做的是把它放在我包中的一个文本文件中。在代码中,当应用程序运行时,我会打开文本文件(作为NSString)并将其拆分为NSArray。现在我已经有了一系列的URL。然后我会在NSArray中循环。现在我们进入了一个循环。对于数组中的每个项,我将初始化MHGalleryItem,然后使用add

我正在使用创建存储在我的网站上的图像库。当前添加图像的方法(如Github上的示例所示)是

但我想添加数百张图片,而这并不是最理想的方式。对我来说,哪种方法更容易实现这一点


谢谢

您必须从URL列表开始。我要做的是把它放在我包中的一个文本文件中。在代码中,当应用程序运行时,我会打开文本文件(作为NSString)并将其拆分为NSArray。现在我已经有了一系列的URL。然后我会在NSArray中循环。现在我们进入了一个循环。对于数组中的每个项,我将初始化MHGalleryItem,然后使用
addObject:
将其添加到先前创建的NSMutableArray中。因此,我们有一个重复的两行或三行循环,在所有URL中运行

以下是未经测试的伪代码(因此可能包含错误),但它应该给出我建议的结构的总体概念:

NSMutableArray* temp = [NSMutableArray new];
NSString* s =
    [NSString stringWithContentsOfFile:
        [[NSBundle mainBundle] pathForResource:@"urls" ofType:@"txt"]
                                      encoding:NSUTF8StringEncoding error:nil];
NSArray* urls = [s componentsSeparatedByString:@"\n"];
for (NSString* url in urls) {
    MHGalleryItem *item = [[MHGalleryItem alloc] initWithURL:url
                                        galleryType:MHGalleryTypeImage];
    [temp addObject:item];
}
self.galleryDataSource = temp;

循环。如果要在变量名的末尾添加数字,则需要一个循环和/或数组

NSMutableArray * photos = [NSMutableArray new];
NSArray * photoPaths = [NSArray arrayWithContentsOfFile:plistContainingPhotoPaths];
for( NSString * path in photoPaths ){
    NSURL * photoURL = [NSURL URLWithString:path];
    MHGalleryItem * photo = [[MHGalleryItem alloc] initWithURL:photoURL
                                                   galleryType:MHGalleryTypeImage];
    [photos addObject:photo];
}

不要对alloc使用点语法,否则您的代码会起火。

在您的网站上使用命名协议,例如:

www.mywebsite.com/appImageGallery/insertImageNumber

并将insertImageNumber替换为图像的编号。然后添加这个for循环以获取所有图像并将它们添加到数组中

NSMutableArray *mutableGalleryDataSource = [self.galleryDataSource mutableCopy]
for(int i = 0; i < numberOfImagesOnWebsite; i++){    //replace numberOfImagesOnWebsite with the number of images on your website.

    MHGalleryItem *newItem = [MHGalleryItem.alloc initWithURL:[@"www.mywebsite.com/appImageGallery/" stringByAppendingString:[NSString stringWithFormat: @"%i", i]] galleryType:MHGalleryTypeImage];

    [mutableGalleryDataSource addObject:newItem];
}
self.galleryDataSource = mutableGalleryDataSource;
NSMutableArray*mutableGalleryDataSource=[self.galleryDataSource mutableCopy]
对于(int i=0;i<代码> > AdvObjsFoMurray方法在<代码> NSMutableArray < /代码> .< /p>查看NStutabRealRy的基础文档,它允许您使用“代码> AdObjult:< /C>”一次一次添加MgRayReIsIs对象一个对象。你的100个URL来自哪里?仅供参考-在调用
alloc
等方法时,请不要使用属性语法。谢谢!URL是我网站上的所有图像。以image_1.jpg格式命名。好的,我将修复属性语法,这就是示例代码中的语法!那么,你是打算对应用程序中的所有URL进行硬编码,还是打算从服务器动态获取URL列表?如果你稍微扩展一下,这个答案会有用得多。展示它如何应用于问题。
NSMutableArray *mutableGalleryDataSource = [self.galleryDataSource mutableCopy]
for(int i = 0; i < numberOfImagesOnWebsite; i++){    //replace numberOfImagesOnWebsite with the number of images on your website.

    MHGalleryItem *newItem = [MHGalleryItem.alloc initWithURL:[@"www.mywebsite.com/appImageGallery/" stringByAppendingString:[NSString stringWithFormat: @"%i", i]] galleryType:MHGalleryTypeImage];

    [mutableGalleryDataSource addObject:newItem];
}
self.galleryDataSource = mutableGalleryDataSource;