下载图像,在UIImageView和SORT中显示它们

时间:2012-09-02 18:09:12

标签: xcode arrays sorting uiimageview

好的,我对目标C相当新。我有一个应用程序从我的Web服务器下载图像列表,并在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];
}

1 个答案:

答案 0 :(得分:1)

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

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

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