下载图像,在UIImageView中显示它们,然后进行SORT [英] Download Images, Display Them in UIImageView, and SORT

查看:86
本文介绍了下载图像,在UIImageView中显示它们,然后进行SORT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我对目标C还是陌生的.我有一个应用程序,它从网络服务器上下载图像列表,并在UIImage视图中显示适当的图像,并带有滑动手势以向前或向后前进以显示下一个/上一个要显示的图片.图片的当前命名格式如下:

Ok guys, I am fairly new to objective C. I have an app that downloads a list of images from my web server and displays the appropriate ones in a UIImage view with swipe gestures to go forward or backwards for the next/previous pic to be displayed. Current naming format for the pictures is like this:

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

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

The current code loads every picture into the view that has 141 in the middle part of the filename (or whatever record the user in on... 141 is variable in this instance, just showing here for an example of the format). The problem is, there seems to be no rhyme or reason as to what order they are displayed in. I would like it to use the last part of the filename to sort alphabetically (or even the whole filename, as it would achieve the same result). In the example above, it would display the downloaded pics in the following order when swiping through the uiimageiew:

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

我已经搜索了,但是找不到我想要的东西(可能是因为我使用了错误的搜索条件).这是我现有的代码,可下载并在UIImageView中显示图像.我认为排序"代码会放在这里的某个地方:

I've search and can't find what I am looking for (maybe because I'm using the wrong search criteria). Here is my existing code that downloads and displays the images in the UIImageView. I assume the "sort" code would go in here somewhere:

-(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];
}

推荐答案

在开始下载操作之前,应在downloadPictures:方法中对picPaths数组进行排序,使其与所需图像顺序相同.您可以通过使用NSArray方法sortedArrayUsingSelector:创建一个新的排序数组来做到这一点.使用caseInsensitiveCompare:作为排序的选择器将按字母顺序对数组中的NSString进行排序.

In your downloadPictures: method you should sort your picPaths array to be the order you want the images before you start the download operation. You can do this by creating a new sorted array using the NSArray method sortedArrayUsingSelector:. Using caseInsensitiveCompare: as the selector for the sort will order the NSStrings in the array alphabetically.

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

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

Then when you init your NSInvocationOperation, pass the sorted array as the object.

这篇关于下载图像,在UIImageView中显示它们,然后进行SORT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆