我希望能够从我的iPhone应用程序中的Web文件夹中查看图像。我知道如何使用特定网址查看图像(即www.mywebsite.com/image.jpg)。这很简单。我只是不知道如何异步加载数组。基本上我需要查看具有特定序列的图像(即mywebsite.com/image_001.jpg,mage_002.jpg,mage_003.jpg等)。具有该序列的文件夹中可能有10或100个图像。如何让我的应用程序按顺序加载图像?
答案 0 :(得分:0)
以下代码将对此文件格式的图像进行排序" 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);