how to make this code asynchronous??
as this code allow to change image on tap in imageview but right now its slow
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger sections = [indexPath section];
if (sections == 3)
{
ltxt.text = [NSString stringWithFormat:@" %d of %d", tap,[a1 count]];
if(tap<[a1 count]-1) {
tap++;
NSString *sa=[a1 objectAtIndex:tap];
image= [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat: sa,[a1 objectAtIndex:tap ]]]]];
// NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)];
myImageView.image = image;
//[myimg release];
}
it depends on how many images you’ll need to request at any given time. a good solution i found for industrial strength design/requests was to create a request funnel (which reduced the number of images). these requests supported cancellation (for when the image view goes off-screen), and were all handled via an NSOperationQueue. there was a ton of threading work (read: not for the noob) to get it seamless, but it worked great for large tables which had many images (since the ceiling is quite low for the number of images you can fit into physical memory on the iOS device with the least amount of memory).
that’s one ‘right’ way, if you have a lot of images to download. if not, then you can look into implementation using performSelectorInBackground:…, and just let the object perform its own locking/handling. either way, you’ll have to do something/display something while the image is being downloaded, so the calling thread (typically the main thread) is not blocked while the image is received.
Q::follow-up: thanks but where should i declare this method?? inside if (sections == 3) { } – user437503
A::it will take the `general’ form: