简体   繁体   English

在iOS中查看来自Web文件夹的图像数组

[英]View Image array from web folder in iOS

I want to be able to view images from a web folder inside my iPhone app. 我希望能够从我的iPhone应用程序中的Web文件夹中查看图像。 I know how to view the images with a specific url (ie www.mywebsite.com/image.jpg). 我知道如何使用特定网址查看图像(即www.mywebsite.com/image.jpg)。 That's easy. 这很容易。 I just don't know how to asynchronously load an array. 我只是不知道如何异步加载数组。 Basically I need to view images with a specific sequence (ie mywebsite.com/image_001.jpg, image_002.jpg, image_003.jpg, etc). 基本上我需要查看具有特定序列的图像(即mywebsite.com/image_001.jpg,mage_002.jpg,mage_003.jpg等)。 There may be 10 or 100 images in a folder with that sequence. 具有该序列的文件夹中可能有10或100个图像。 How do I let my app load images with a sequence? 如何让我的应用程序按顺序加载图像?

Following code will sort images of this file format "imageName_number.jpg" 以下代码将对此文件格式“imageName_number.jpg”的图像进行排序

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *webDir=[documentsDirectory stringByAppendingPathComponent:@"WebFolder"];
    NSFileManager *filemgr;


    NSMutableArray *fileNum;
    fileNum=[[NSMutableArray alloc]init];


    filemgr = [NSFileManager defaultManager];

    //This will give all files of your web directory you can uncomment to get dynamically
    //NSArray *filelist = [filemgr contentsOfDirectoryAtPath: webDir error: nil];
    // this is just example shows how unsorted will be used to sort image no you can comment this line
    NSArray *filelist=[[NSArray alloc]initWithObjects:@"abc_001.jpg",@"def_005.jpg",@"abc_002.jpg",@"abc_0103.jpg",@"abc_0010.jpg",@"abc_008.jpg", nil];
   int count = (int)[filelist count];

  NSMutableDictionary *dictFileNumWithPath=[[NSMutableDictionary alloc]init];

   NSString *imageSeprator=@"_";

    for (int i = 0; i < count; i++){
        NSString *imageName=[filelist objectAtIndex: i];
        if (!([imageName rangeOfString:imageSeprator].location == NSNotFound)) {

            NSRange startRange = [imageName rangeOfString:imageSeprator];
            NSRange endRange = [imageName rangeOfString:@".jpg"];

            NSRange searchRange = NSMakeRange( startRange.location+1, endRange.location-endRange.length);
            NSString *strNum= [imageName substringWithRange:searchRange];

            NSString *webPath=[webDir stringByAppendingPathComponent:imageName];
            [dictFileNumWithPath setObject:[NSNumber numberWithInt:[strNum intValue]] forKey:webPath];

        }
    }

    NSArray *sortedKeysFilePathArray =
    [dictFileNumWithPath keysSortedByValueUsingSelector:@selector(compare:)];

    NSLog(@"%@", sortedKeysFilePathArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM