tags:

views:

47

answers:

1

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];
}
+1  A: 

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:

- (void)setCellImage:(UIImage *)img {
/* assert here if called from secondary thread */
    myImageView.image = img;
}

- (void)udpateImageFollowingTap {
    NSAutoreleasePool * pool = [NSAutoreleasePool new];
    NSString * imageUrl = [self.a1 objectAtIndex:self.tap];
    UIImage * tmpImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]]];
    [self performSelectorOnMainThread:@selector(setCellImage:) withObject:tmpImage];
    [tmpImage release];
    [pool release];
}

- (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++;
            /* insert code to discard previous image and display loading image indication here */
            [self performSelectorInBackground:@selector(udpateImageFollowingTap) withObject:0];
        }
    }
}
Justin
if you need one image per tap/user event, then performSelectorInBackground using a method is going to be preferred.
Justin
thanks but where should i declare this method?? inside if (sections == 3) { }
prajakta
thanks but i am getting this error [BookDetailViewController performSelectorOnMainThread:withObject:]: unrecognized selector sent to instance 0x1ab8f0i think [self performSelectorOnMainThread:@selector(setCellImage:) withObject:tmpImage]; is something wrong
prajakta
if i do [self performSelectorInBackground:@selector(setCellImage:) withObject:tmpImage]; then code is working but still i cant display images but if i use mainThread then app quits
prajakta
fixed the problem hold on 1 min
prajakta
perfect thanks a lot i have one more issue but i will ask in other thread :)
prajakta
you're welcome =)
Justin