0

好的,伙计们,我对目标 C 很陌生。我有一个应用程序,它从我的网络服务器下载图像列表,并在 UIImage 视图中显示适当的图像,并通过滑动手势向前或向后移动下一张/上一张图片显示。目前图片的命名格式是这样的:

uploaded_141_admin1.png
uploaded_141_admin2.png
uploaded_141_interior1.png
uploaded_141_interior2.png
uploaded_141_exterior1.png

当前代码将每张图片加载到文件名中间部分有 141 的视图中(或者用户在... 141 上的任何记录在这种情况下是可变的,此处仅显示格式示例)。问题是,它们的显示顺序似乎没有押韵或原因。我希望它使用文件名的最后一部分按字母顺序排序(甚至整个文件名,因为它会达到相同的结果)。在上面的示例中,它会在滑动 uiimageiew 时按以下顺序显示下载的图片:

uploaded_141_admin1.png
uploaded_141_admin2.png
uploaded_141_exterior1.png
uploaded_141_interior1.png
uploaded_141_interior2.png

我已经搜索但找不到我要找的东西(可能是因为我使用了错误的搜索条件)。这是我现有的代码,用于下载并显示 UIImageView 中的图像。我假设“排序”代码会在这里某处:

-(void)downloadPictures:(NSArray *)picPaths {
    ELog(@"Downloading pictures: %@",picPaths);
    // wait indicator
    [[WaitingView sharedInstance] setMessage:LocStr(@"Loading pictures... The more pictures there are, the longer this will take.  Please be patient.")];
    [[WaitingView sharedInstance] showIndicator:YES];
    [[WaitingView sharedInstance] displayOn:[self view]];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    //  queue download operation 
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    NSInvocationOperation *downloadOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadOperation:) object:picPaths];
    [queue addOperation:downloadOp];
}

-(void)downloadOperation:(NSArray *)picPaths {
    NSMutableArray *allPictures = [[NSMutableArray alloc] init];
    for(NSString *path in picPaths) {
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@%@/%@/%@",SERVER_ADDRESS,SERVER_PORT,SERVER_PHOTOS,path]];
        NSData *picData = [NSData dataWithContentsOfURL:url];
        if(picData!=nil) {
            UIImage *img = [UIImage imageWithData:picData];
            if(img!=nil) {
                [allPictures addObject:img];    
            } else {
                ELog(@"Failed to convert data to image from url %@",url);
            }
        } else {
            ELog(@"Failed to download image from url %@",url);
        }
    }
    [[WaitingView sharedInstance] performSelectorOnMainThread:@selector(remove) withObject:nil waitUntilDone:NO];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    self.pictures=allPictures;
    if([self.pictures count]==0) {
        [self performSelectorOnMainThread:@selector(downloadErrorMessage) withObject:nil waitUntilDone:NO];
    } else {
        self.currentIndex=0;
        [self performSelectorOnMainThread:@selector(showPicture) withObject:nil waitUntilDone:NO];
    }
}

-(void)downloadErrorMessage {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oooops!" message:LocStr(@"Pictures download failed") delegate:nil cancelButtonTitle:LocStr(@"Close") otherButtonTitles:nil];
    [alert show];
    [alert release];    
    [self goBack];
}

-(void)showPicture {
    UIImage *image = [self.pictures objectAtIndex:self.currentIndex];
    ELog(@"Now displaying image with index %d: %@",self.currentIndex,image);
    self.picture.image=image;
    [self.picture setNeedsLayout];
}
4

1 回答 1

1

在您的downloadPictures:方法中,您应该picPaths在开始下载操作之前将您的数组排序为您想要的图像顺序。您可以通过使用 NSArray 方法创建一个新的排序数组来做到这一点sortedArrayUsingSelector:。用作排序的选择器caseInsensitiveCompare:将按字母顺序排列数组中的 NSString。

NSArray *sortedPicPaths = [picPaths sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

然后,当您初始化 NSInvocationOperation 时,将排序后的数组作为对象传递。

于 2012-09-02T18:34:11.233 回答