0

我正在尝试将 5 或 6 个遥控器图像加载到滚动视图并水平滑动。我可以从我的网络服务器(JSON 输出)中获取 URL,我正在尝试使用 AFnetworking 来解决性能问题。

到目前为止,我可以获得 URL 并将一张图片加载到这样的 UIImageview (使用 Storyboard )

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

谢谢你的帮助 。

4

3 回答 3

0

[编辑] 根据您在下面的评论,我实际上可能会选择一种不同的方法来引入您的所有图像。此时,您实际上可以使用 UIImageView 或 UIButton 来获得您想要的点击(从滚动视图)。将 UIImage 分配给您选择的 UIButton 或 UIImageView,然后根据需要将它们放置在滚动视图上。您必须为每个 Button 或 ImageView 动态创建操作。然后,您的操作创建应该会在您的滚动视图或您动画到的另一个视图控制器等上方显示另一个视图。在视图控制器中,您将 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/

于 2012-06-01T14:43:21.887 回答
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)]];
于 2012-06-01T21:36:12.650 回答
0

如果您使用 AFNetworking Conect,则使用 Image Request 概念:- 试试这个代码:-

 - (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] ;
    }
于 2012-06-02T07:43:37.710 回答