In my opinion, I have an image representation, the data for viewing images comes from Url, the images are about 1-3 MB. If the user is Swipes, then I want to download the following image. Each thing works fine if you scroll slowly, but when I quickly checked, I want to undo the previous operation and start with a new URL.
For example. if the user commits 4 times, if the operations for the 2nd and 3rd images are in the middle, I want to cancel them and start downloading the 4th image
But now, instead of the 4th image, Im, the first first image follows the 3rd, and then the 4th image appears.
Here is my sample code
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer { [BackgroundOperation cancelAllOperations]; // To cancel previous one [self performSelector:@selector(LoadImage) withObject:nil afterDelay:0.1]; } -(void)LoadImage { BackgroundOperation=[[NSOperationQueue alloc]init]; imgvww.image=[UIImage imageNamed:@"loader.png"]; // Place holder till download finishes [BackgroundOperation addOperationWithBlock:^ { UIImage *img=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[self.ItemDetails objectAtIndex:0] objectForKey:@"ImageUrl"]]]]; // Getting data from URL [[NSOperationQueue mainQueue] addOperationWithBlock:^{ imgvww.image=img; //Adding to image view after completion }]; }]; }
Thanks.
ios nsoperationqueue cancellation
siva krishna
source share