2012-06-01 39 views
0

我想加載5或6遙控器圖像滾動視圖和水平滑動。我可以從我的網絡服務器(JSON輸出)獲取URL,我正在嘗試使用AFnetworking來處理性能問題。如何加載遠程圖像並保存到NSMutableArray

到目前爲止,我可以得到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

請使用延遲加載概念 – Deepesh

+0

我沒有得到這個問題,你試圖達到什麼樣的行爲? –

+0

加載5個圖像以滾動查看並滾動/掃描圖像。謝謝 – HernandoZ

回答

0

[編輯]基於下面您的意見 ,我可能真的然後選擇不同的方法 在所有的圖片帶上。此時,您可以使用UIImageView或UIButton實際獲取您希望的點擊(從滾動視圖)。將UIImage分配給您選擇的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/

+0

嗨謝謝,但我不想使用表視圖,我想使用滾動視圖和分頁。 – HernandoZ

+0

Scrollviews仍然必須在其中有某種視圖。你總是可以擁有一個通用的UIView作爲你的畫布,並將UIImage放置在UIView上 – trumpetlicks

+0

因此,你的UIView作爲ScrollView的子視圖,然後你將UIImage分配給UIImageViews,使用addSubview例程。從那裏你可以將它們放置在UIView「canvas」所需的框架屬性中。 – trumpetlicks

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)]]; 
+0

我有問題是將圖像保存到NSMutableArray?謝謝。 – HernandoZ

+0

+(void)createCarWithImage:(NSString *)theImageName {UIImage * anImage = [UIImage imageNamed:theImageName]; [theArray addObject:anImage]; }我從這篇文章得到這個http://stackoverflow.com/questions/6005870/how-to-add-uiimages-to-an-nsmutablearray-and-change-the-images-later – Kibitz503

0

如果你使用瞬移AFNetworking,使用圖像請求概念: - 試試這個代碼: -

- (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] ; 
    } 
相關問題