我是iPhone编程的新手。使用下面的代码,我可以在后台快速和异步地从服务器获取图像。
我想以15张图片的形式下载图片。一旦用户向下滚动,他就可以下载并查看下15个图像。此外,我想卸载并删除不再显示的图像。所以我想分批下载图片,如果有1000张图片,我希望他们下载为0-15,15-30,30-45,...等等。
我该如何实现?
使用Lazyloading或SDWebimage源代码项目,我能够像我说的那样获取图像,但我想以3 * 3缩略图显示图像(意味着我每行要显示3张图像)请告诉我怎么办?像这样。我想要像Android中的通用库这样的lib
//remote image URLs
URLs = [[NSMutableArray alloc]init];
for (NSString *path in latestiamge)
{
NSURL *URL = [NSURL URLWithString:path];
if (URL)
{
[URLs addObject:URL];
}
else
{
}
}
self.imageURLs = URLs;
UISwipeGestureRecognizer* swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeUpFrom:)];
swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[myScrollView addGestureRecognizer:swipeUpGestureRecognizer];
float horizontal = 2.0;
float vertical = 2.0;
for(int i=0; i<[imageURLs count]; i++)
{
if((i%3) == 0 && i!=0)
{
horizontal = 5.0;
vertical = vertical + 100.0 + 5.0;
}
CGRect frame;
frame.size.width=100.0;
frame.size.height=100.0;
frame.origin.x=0;
frame.origin.y=0;
AsyncImageView *imageView = [[AsyncImageView alloc] initWithFrame:frame];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
imageView.tag = i;
//load the image
imageView.imageURL = [imageURLs objectAtIndex:i];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(actionHandleTapOnImageView:)];
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:singleTap];
[myScrollView addSubview:imageView];
buttonImage1 = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonImage1 setFrame:CGRectMake(horizontal, vertical, 100.0, 100.0)];
[buttonImage1 setTag:i];
[buttonImage1 setImage:[idURLs objectAtIndex:i] forState:UIControlStateNormal];
[buttonImage1 setImage:[UIImage imageNamed:@"Overlay.png"] forState:UIControlStateSelected];
[myScrollView addSubview:buttonImage];
[buttonImage1 addSubview:imageView];
[myScrollView addSubview:buttonImage1];
horizontal = horizontal + 100.0 + 5.0;
}
[myScrollView setContentSize:CGSizeMake(320.0, vertical + 3900.0)];
答案 0 :(得分:4)
如果您有兴趣了解所有工作背后的概念..请仔细阅读以获得基本的理解:
http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues
如果您只想使用异步加载。我建议你用这个。它基于UIImage。这是非常好的
https://github.com/rs/SDWebImage.git
我建议你调查所有这些并享受乐趣。