Wednesday, November 27, 2013

Loading local images from documents directory to tableview using SDWebImage http://stackoverflow.com/questions/12792704/loading-local-images-from-documents-directory-to-tableview-using-sdwebimage

http://stackoverflow.com/questions/12792704/loading-local-images-from-documents-directory-to-tableview-using-sdwebimage

Loading local images from documents directory to tableview using SDWebImage


I am using a tableview which loads images from the documents directory, creates a thumbnail and shows it in the tableview. However, I have a problem: it becomes slow and crashes as the pictures are large, taken using the camera.

I have explored several solution including GCD to do the work in a background thread but the result is the same thing. So, I thought to look into SDWebImage but I don't know if it will also work for local files, not web images in this case. Can someone advise me please? If not, how is this problem solved? Is there an API that can help to resolve this issue?

asked Oct 9 '12 at 4:12
Paulo
8629
add comment

That question is not easy to answer as the Question is asked fairly broad but I will do my best.

First, I usually dispatch a Background Thread if I have expensive processing to do as to not block the Main Thread, which is fairly important. I don't really know why you are not using the normal UIImageView for what you are doing but try to implement following :

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {      static NSString *CellIdentifier = @"YourCell";      MyCellClass *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];      if (cell == nil) {          cell = [[MyCellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];      }  /*   Whatever Code you want   */      NSArray* params =@[cell.myImageView, @"http://myfancyimages.com/image.png"];      [self performSelectorInBackground:@selector(loadEventImageWithParameters:) withObject:params];      return cell;  }

And now add the function :

  - (void) loadEventImageWithParameters:(id) parameters {      NSArray* params = [[NSArray alloc] initWithArray:(NSArray*)parameters];      NSURL *url = [NSURL URLWithString:[params objectAtIndex:0]];      UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];      UIImageView* theImageView = (UIImageView*) [params objectAtIndex:0];      [theImageView setImage:image];  }

If you got a lot of Pictures to load you are well advised to queue your Processes so you don;t "steal" all resources with Grand Central Dispatch. Have a read through this excellent posthttp://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial for further details.

Hope that helped

No comments:

Post a Comment