【问题标题】:Download Images, Display Them in UIImageView, and SORT下载图片,在 UIImageView 中显示,并排序
【发布时间】:2012-09-02 18:09:12
【问题描述】:

好的,伙计们,我对 Objective 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];
}

【问题讨论】:

    标签: xcode arrays sorting uiimageview


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 2020-05-13
      • 1970-01-01
      • 2020-06-29
      相关资源
      最近更新 更多