JSON Image Parsing / Prefixed URL

I installed a successful analysis of text and images in a table view using JSON / PHP / MYSQL. I only save the location of the images in the database, and the actual images are stored in a directory on my server. The only thing that is stored regarding the image in the database is the name. Example car.jpg. What I want to do is prefix the URL of the image location on my server so that they can be parsed without having to enter the database and manually enter the URL. Here are some of my code ...

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identifier = @"studentsCell"; StudentsCell *cell = (StudentsCell *)[tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StudentsCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; } NSDictionary *studentsDict = [students objectAtIndex:indexPath.row]; //I want to prefix the URL for the key imagepath but i dont know where and how to do it. NSURL *imageURL = [NSURL URLWithString:[studentsDict objectForKey:@"imagepath"]]; NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; UIImage *imageLoad = [[UIImage alloc] initWithData:imageData]; cell.imageView.image = imageLoad; NSString *name = [NSString stringWithFormat:@"%@ %@", [studentsDict valueForKey:@"first"], [studentsDict valueForKey:@"last"]]; cell.title.text = name; NSString *subtitle = [NSString stringWithFormat:@"%@", [studentsDict objectForKey:@"email"]]; cell.subtitle.text = subtitle; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(265, 6, 44, 44); [button setImage:[UIImage imageNamed:@"email.png"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(email:) forControlEvents:UIControlEventTouchUpInside]; [cell.contentView addSubview:button]; // cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellbackground.png"]]; return cell; } 
+4
source share
1 answer

Suppose you had something like:

 NSURL *baseURL = [NSURL URLWithString:@"http://www.your.site.here/images"]; // whatever the folder with the images is 

Then you could do:

 NSURL *imageURL = [baseURL URLByAppendingPathComponent:[studentsDict objectForKey:@"imagepath"]]; 

By the way, you should consider using the UIImageView category, such as SDWebImage . Then, instead of loading NSData with image data synchronously, you can perform asynchronous image loading:

 [cell.imageView setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

Placeholder is what should be displayed during image loading (maybe just an empty image), and SDWebImage will asynchronously retrieve the image and update the cell when it is retrieved. This will provide a much more flexible user interface. It will also use image caching (so if you scroll down and then back up, the image will not be restored again).

AFNetworking has a similar UIImageView category, but not an equally reliable implementation. But if you are already using AFNetworking, this is an option.

+2
source

Source: https://habr.com/ru/post/1496683/


All Articles