Download Images, Display Them in UIImageView, and

2019-08-27 08:03发布

问题:

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

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

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

回答1:

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:)];

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