在我的视图中,我有一个图像视图,该图像视图的数据来自URL,图片大小在1-3 MB左右。
如果用户滑动,我想加载下一张图片。如果缓慢地滑动,一切都正常,但是当我快速滑动时,我希望取消先前的操作并以新的URL开始。
例如,如果用户滑动4次,如果第2和第3个图像的操作在中途,我想取消这些操作并开始下载第4个图像。
但现在,在第4个图像的位置上,我得到第2个图像和第3个图像,然后才出现第4个图像。
以下是我的示例代码:
谢谢。
例如,如果用户滑动4次,如果第2和第3个图像的操作在中途,我想取消这些操作并开始下载第4个图像。
但现在,在第4个图像的位置上,我得到第2个图像和第3个图像,然后才出现第4个图像。
以下是我的示例代码:
- (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
}];
}];
}
谢谢。