我正在尝试将5或6个遥控器图像加载到滚动视图并水平滑动。我可以从我的网络服务器(JSON输出)获取URL,我正在尝试使用AFnetworking来解决性能问题。
到目前为止,我可以获取URL并将一张图片加载到这样的UIImageview(使用故事板)
Jsonobject *items = [posterNSMarray objectAtIndex:0]; //load one url only
NSlog (@"Object url %@", items.pUrl); // picture URL
[self.imageview setImageWithURl :[NSURL URLWithString:items.pUrl] placeholderImage :[UIImage imageNamed:@"placeholder"]];
[编辑问题] 我的问题是如何获取所有图像并将其保存到NSMutableArray
感谢您的帮助。
答案 0 :(得分:0)
<强> [编辑] 强> 根据您在下面的评论,我可能会选择一种不同的方法 带上你的所有图像。此时,您可以使用UIImageView或UIButton实际获取您希望的tap(来自scrollview)。将UIImages分配给您选择的UIButton或UIImageView,并根据需要将它们放在滚动视图中。您必须为每个Button或ImageView动态创建操作。然后,您的动作创建应该将另一个视图放在您的scrollview上,或者您动画制作的另一个视图控制器等等。在视图控制器中,您将UIImageView作为全屏显示。此时,您可以实现自己的UIGestureRecognizers来实现双击等......
我会使用UIListView。这是自定义UIListView实现的一个很棒的链接。
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
我会将您的图像加载到NSMutableArray中,供自定义UITableView的绘图例程使用。
对于滑动,您可以捕获滑动事件,然后您可以根据需要使用列表项(即从图像阵列中删除,编辑等)。
另一个链接: http://www.icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/
答案 1 :(得分:0)
从您的问题和评论中,您似乎想要加载带有多个图像的UIScrollView,然后轻扫每个图像。听起来你也想要能够点击一个并让它发出一张爆炸的图像供用户查看。
我为一个旧项目写了一些这些函数(它们有点粗糙),但是你可以使用它们,因为它是我完成我认为你所要求的。
-(void)setupPictures
{
imageSectionSlider = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, IMAGE_HEIGHT)];
imageSectionSlider.showsVerticalScrollIndicator = NO;
imageSectionSlider.showsHorizontalScrollIndicator = NO;
imageSectionSlider.pagingEnabled = YES;
imageSectionSlider.bounces = NO;
UIView* tilesHolder = [[UIView alloc]initWithFrame:CGRectMake(0, 0, (([[thisStory imageList]count] * (self.frame.size.width))), IMAGE_HEIGHT)];
tilesHolder.userInteractionEnabled = YES;
for (int count = 0; count < [[thisStory imageList]count]; count++)
{
[tilesHolder addSubview:[self createImageTile:[[thisStory imageList]objectAtIndex:count] Count:count Rect:CGRectMake( 320*count , 0, 320, IMAGE_HEIGHT)]];
}
[imageSectionSlider setContentSize:CGSizeMake( tilesHolder.frame.size.width , tilesHolder.frame.size.height)];
[imageSectionSlider addSubview:tilesHolder];
[tilesHolder release];
}
-(UIView*)createImageTile:(ImageItem*)input Count:(int)count Rect:(CGRect)rect
{
UIView* imageTile = [[UIView alloc]initWithFrame:rect];
[imageTile setTag:count];
UIImageView* image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, imageTile.frame.size.width, imageTile.frame.size.height - 45)];
[image setImage:[input imageData]];
image.contentMode = UIViewContentModeScaleAspectFill;
image.clipsToBounds = YES;
image.userInteractionEnabled = YES;
image.tag = count;
UIGestureRecognizer* featureImageGesRec = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(countTaps:)];
[image addGestureRecognizer:featureImageGesRec];
[featureImageGesRec release];
[imageTile addSubview:image];
[image release];
return [imageTile autorelease];
}
- (void)countTaps:(NSObject*)sender {
tapCount++;
if (tapCount == 1) {
//do something with single tap
}
else if (tapCount == 2)
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self doubleTap:sender];
}
}
-(void)doubleTap:(NSObject*)sender
{
UITapGestureRecognizer* item = (UITapGestureRecognizer*)sender;
tapCount = 0;
//FullSizeImage
ImageItem* selectedItem = [[thisStory imageList]objectAtIndex:item.view.tag];
ExpandedView* pictureView = [[ExpandedView alloc]initWithImage:[selectedItem imageData]];
[thisParent.navigationController pushViewController:pictureView animated:YES];
[pictureView release];
}
只需在此行传递您的异步加载图片...
[tilesHolder addSubview:[self createImageTile:/*Image*/ Count:count Rect:CGRectMake( 320*count , 0, 320, IMAGE_HEIGHT)]];
答案 2 :(得分:0)
如果您使用AFNetworking Conect,则使用图像请求概念: - 试试这段代码: -
- (void)setupPage
{
scrollPage = 0 ;
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0f,0, 320,367)];
[scrollView setBackgroundColor:[UIColor clearColor]];
[scrollView setDelegate:self];
[self.view addSubview:scrollView];
NSUInteger nimages = 0;
CGFloat cx = 0;
CGFloat cy = 0;
for (; nimages < [stealArr count]; nimages++)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,320.0f,367)];
CGRect rect = imageView.frame;
rect.size.height = 331;
rect.size.width = 320.0f;
rect.origin.y = 0.0f;
rect.origin.x = 0.0f+cx;
imageView.frame = rect;
[imageView setBackgroundColor:[UIColor clearColor]];
imageView.contentMode = UIViewContentModeScaleToFill;
[scrollView addSubview:imageView];
[imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
cx += scrollView.frame.size.width;
cy += scrollView.frame.size.height;
}
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 331.0f, 320, 36)] ;
[pageControl setCurrentPage:0] ;
[pageControl addTarget: self action: @selector(pageControlClicked:) forControlEvents: UIControlEventValueChanged] ;
[pageControl setDefersCurrentPageDisplay: YES];
[pageControl setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:pageControl];
pageControl.numberOfPages = [stealArr count];
[scrollView setContentSize:CGSizeMake(cx,[scrollView bounds].size.height)];
[scrollView setPagingEnabled:TRUE];
}
when Your are scroll the page
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = scrollView.frame.size.width;
scrollPage = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = scrollPage;
be to unload the views+controllers which are no longer visible
}
for Paging
- (void)pageControlClicked:(id)sender
{
UIPageControl *thePageControl = (UIPageControl *)sender ;
// we need to scroll to the new index
[scrollView setContentOffset: CGPointMake(scrollView.bounds.size.width * thePageControl.currentPage, scrollView.contentOffset.y) animated: YES] ;
}