【发布时间】:2015-11-23 19:37:12
【问题描述】:
在我看来,我有图像视图,图像视图的数据来自 Url,图像大约 1-3 MB。 如果用户滑动然后我想加载下一张图片,如果慢慢滑动,一切都很好,但是当我快速滑动时,我想取消之前的操作并从新的 url 开始。
例如。如果第二张和第三张图片的操作在中间,用户滑动 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
}];
}];
}
谢谢。
【问题讨论】:
-
请努力格式化您的代码,以便其他人可以阅读。
-
您是否在每次需要加载图像时都重新创建
BackgroundOperation(使用BackgroundOperation=[[NSOperationQueue alloc]init];)?
标签: ios nsoperationqueue cancellation