2013-12-18 46 views
0

我有20個圖像在我的主包,我想顯示它在我的滾動視圖水平。ScrollView未更新imageview

我成功地做到了,但現在問題是所有imageview都設置在滾動視圖中後,滾動視圖中可見圖像,並且需要時間。

爲了解決這個問題,我創建了一個nsoperation。

-(BOOL)sync:(NSError**)error 
{ 
    float imageLeftPosition = 0; 

    for (int imageCount = 1; imageCount<=syncObject.syncNumberOfImages; imageCount++) { 

     UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(imageLeftPosition, 0, syncObject.syncImageWidth, syncObject.syncImageHeight)]; 

     [imgView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d_%d.jpg",syncObject.syncChapterNumber,imageCount]]]; 

     imgView.tag = imageCount; 

//  NSDictionary *dictionary = [NSDictionary new]; 
//   
//  [dictionary setValue:imgView forKey:CHAPTER_IMAGE]; 

     imageLeftPosition = imageLeftPosition+syncObject.syncImageWidth; 

     //[dictionary setValue:[NSString stringWithFormat:@"%f",imageLeftPosition] forKey:CHAPTER_SCROLL_LEFT_POSITION]; 

     NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:imgView,[NSString stringWithFormat:@"%f",imageLeftPosition], nil] forKeys:[NSArray arrayWithObjects:CHAPTER_IMAGE,CHAPTER_SCROLL_LEFT_POSITION, nil]]; 

     [self sendNotification:dictionary]; 

     imgView = nil; 
     dictionary = nil; 

    } 

    return YES; 
} 

什麼,我在這裏做的就是添加創建一個圖像查看和使用的通知張貼到主視圖

-(void)addImageView:(NSNotification*)notification 
{ 
    NSInteger thread = [NSThread isMainThread]?CVC_MainThread:CVC_BackgroundThread; 
    switch (thread) { 
     case CVC_MainThread: 
     { 
      NSDictionary *dictionary = notification.userInfo; 
      if(dictionary != nil) 
      { 
       NSLog(@"image view : %@",(UIImageView*)[dictionary valueForKey:CHAPTER_IMAGE]); 
       NSLog(@"leftposition: %f",[[dictionary valueForKey:CHAPTER_SCROLL_LEFT_POSITION] floatValue]); 
       [_scrChapters addSubview:((UIImageView*)[dictionary valueForKey:CHAPTER_IMAGE])]; 
       _scrChapters.contentSize = CGSizeMake([[dictionary valueForKey:CHAPTER_SCROLL_LEFT_POSITION] floatValue], 0); 
      } 
      dictionary = nil; 
     } 
      break; 

     case CVC_BackgroundThread: 
     { 
      dispatch_async(dispatch_get_main_queue(), ^(void) { 
       NSDictionary *dictionary = notification.userInfo; 
       if(dictionary != nil) 
       { 

        NSLog(@"image view : %@",(UIImageView*)[dictionary valueForKey:CHAPTER_IMAGE]); 
        NSLog(@"leftposition: %f",[[dictionary valueForKey:CHAPTER_SCROLL_LEFT_POSITION] floatValue]); 
        [_scrChapters addSubview:((UIImageView*)[dictionary valueForKey:CHAPTER_IMAGE])]; 
        _scrChapters.contentSize = CGSizeMake([[dictionary valueForKey:CHAPTER_SCROLL_LEFT_POSITION] floatValue], 0); 

       } 
       dictionary = nil; 
      }); 
     } 
      break; 

     default: 
      break; 
    } 
} 

這是通知處理方法,在這裏我設置的內容大小和圖像視圖中滾動在加載所有圖像後,主線程靜態滾動視圖上的每個循環的視圖都可見。

我做錯了什麼?

+0

是內容大小是在該正確[[字典valueForKey:CHAPTER_SCROLL_LEFT_POSITION]的floatValue]使用靜態值作爲圖像數*圖像尺寸交叉檢查。 –

回答

0

嘗試此

-(void)showimagesasSlide:(int) totalimagesCount 
{ 
    int xAxisofimageView=0; 

    for (int initialimagesCount=0;initialimagesCount<totalimagesCount; initialimagesCount++) { 
     UIImageView *detailedImageview=[[UIImageView alloc] init]; 
     detailedImageview.userInteractionEnabled=YES; 

     UIImage *dynamicImage=[ScaleImageSize squareImageWithImage:[images objectAtIndex:initialimagesCount] scaledToSize:CGSizeMake(300, 500) ]; 


     if (IS_IPHONE_5) { 
      detailedImageview.frame=CGRectMake(xAxisofimageView, 0, 320, 568); 

     } 
     else 
     { 
      detailedImageview.frame=CGRectMake(xAxisofimageView, 0, 320, 460); 

     } 
    // detailedImageview.contentMode=UIViewContentModeCenter; 


     detailedImageview.backgroundColor=[UIColor clearColor]; 
     detailedImageview.image=dynamicImage; 
     detailedImageview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin ; 
     detailedImageview.tag=initialimagesCount+12; 

     [mainScrollview addSubview:detailedImageview]; 
     xAxisofimageView=xAxisofimageView+320; 

     } 
    mainScrollview.contentSize=CGSizeMake(320*images.count, 0); 
}