2011-04-12 91 views
0

我使用performSelectorInBackground从URL中获取图像,将图像加载到UIscrollview内的UIView中。图像正在正确地下载到数组中,但是当我分配给UIImageView这些图像时,所有图像都被分配到滚动视图的最后一页。iphone - performSelectorInBackground image加载

@implementation DetailViewController 

- (id)initWithDataSource:(id<TabBarElementProtocol,UITableViewDataSource>)theDataSource { 
    if ([self init]) { 


     // Scroll View 
     scrollView = [[UIScrollView alloc]init]; 

     //a page is the width of the scroll view 

     scrollView.delegate = self; 
     scrollView.pagingEnabled = YES; 
     scrollView.frame = CGRectMake(0, 0, 320, 300); 
     scrollView.contentSize = CGSizeMake(320*10, 300); 
     scrollView.backgroundColor = [UIColor clearColor]; 
     scrollView.showsHorizontalScrollIndicator = NO; 
     scrollView.showsVerticalScrollIndicator = NO; 
     scrollView.scrollsToTop = NO; 
     scrollView.bounces = NO; 
     scrollView.showsHorizontalScrollIndicator = NO; 

     scrollView.pagingEnabled = YES; 

     [self.view addSubview:scrollView]; 

     scrollView.contentOffset = CGPointMake(10*320, 0); 

     [self displayView]; 

    } 
    return self; 
} 




-(void) displayView{ 
    DouglasAppDelegate *delegate = (DouglasAppDelegate *)[[UIApplication sharedApplication] delegate]; 


    for (int i=0; i < 10; i++) 
    { 

     myview = [[UIView alloc] init]; 
     myview.frame = CGRectMake(320*i , 0, 320, 300); 
     [myview setBackgroundColor: [UIColor whiteColor]]; 


     pictureImageView = [[UIImageView alloc] initWithFrame:CGRectMake(179, 11, 100, 100)]; 
     pictureImageView.tag = i; 
     [myview addSubview: pictureImageView]; 
     [pictureImageView release]; 
// Picture URL is accessed dynamically and loading correctly. 
     [dataArray insertObject:[NSMutableArray arrayWithObjects:picUrl,[NSString stringWithFormat:@"%d", i],nil]  atIndex:i]; 


     [scrollView addSubview:myview]; 

    } 

    [self performSelectorInBackground:@selector(loadImageInBackground:) withObject:dataArray]; 


    //Pager control 

    tpageControl     = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 300 , 320, 17)]; 
    tpageControl.backgroundColor = [UIColor clearColor]; 
    tpageControl.numberOfPages  = [delegate.NeuEntries count]; 
    tpageControl.currentPage  = delegate.neuDetailIndex; 
    zumPage       = delegate.neuDetailIndex ; 
    [tpageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged]; 
    [self.view addSubview: tpageControl]; 

} 


- (void) loadImageInBackground:(NSMutableArray *)urlAndTagReference { 

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    NSData *imgData; 
    UIImage *img; 
    NSMutableArray *arr1 = [[NSMutableArray alloc] initWithCapacity: [urlAndTagReference count]]; 

    for(int i=0; i<[urlAndTagReference count] ;i++) 
     { 
      NSString *url = [[urlAndTagReference objectAtIndex:i] objectAtIndex:0]; 
      NSURL *imgURL = [NSURL URLWithString:url];  
      imgData = [NSData dataWithContentsOfURL:imgURL]; 
      img = [[UIImage alloc] initWithData:imgData]; 
      [arr1 insertObject:[NSMutableArray arrayWithObjects:img,[[urlAndTagReference objectAtIndex:i]objectAtIndex:1],nil] atIndex:i]; 

    } 

    [self performSelectorOnMainThread:@selector(assignImageToImageView:) withObject:arr1 waitUntilDone:YES]; 
} 



- (void) assignImageToImageView:(NSArray *)imgAndTagReference 
{ 

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 


    for(int i=0; i<[imgAndTagReference count] ;i++){ 

      UIImage *img = [[UIImage alloc] init]; 
      img = [[imgAndTagReference objectAtIndex:i] objectAtIndex:0]; 
      NSLog(@"img %@",img); 
      // SAME IMAGES OVERLAPPING ON LAST PAGE OF SCROLL VIEW???????????????? 
      [pictureImageView setImage:img]; 
     } 



} 


- (void) pageTurn:(UIPageControl*) aPageController{ 

    int whichPage     = aPageController.currentPage; 
    scrollView.contentOffset  = CGPointMake(320.0f * whichPage, 0.0f); 

} 



- (void)scrollViewDidScroll:(UIScrollView *)sender 
{ 

    CGPoint offset    = sender.contentOffset; 
    tpageControl.currentPage = offset.x/320.0f; 


} 

回答

0

在您创建一个存储下面一行泄漏

UIImage *img = [[UIImage alloc] init]; 
img = [[imgAndTagReference objectAtIndex:i] objectAtIndex:0]; 

你不需要alloc和初始化一个新的UIImage只是将它设置为在第二行的值。

当你在循环图像时,你正在设置同一个物体上的所有图像pictureImageView。您需要存储一个pictureImageView数组,并按照您执行图像的相同方式对它们进行循环,并将每个图像分配给相应的图像视图。

for (int i=0; i < 10; i++) 
{ 
    //It looks like myview is an ivar as well and should only be local 
    myview = [[UIView alloc] init]; 
    myview.frame = CGRectMake(320*i , 0, 320, 300); 
    [myview setBackgroundColor: [UIColor whiteColor]]; 


    UIImageView *tmp = [[UIImageView alloc] initWithFrame:CGRectMake(179, 11, 100, 100)]; 
    //NSMutableArray declared in header and already allocated 
    [pictureImageViewArray addObject:tmp]; 
    tmp.tag = i; 
    [myview addSubview: tmp]; 
    [tmp release]; 
    // Picture URL is accessed dynamically and loading correctly. 
    [dataArray insertObject:[NSMutableArray arrayWithObjects:picUrl,[NSString stringWithFormat:@"%d", i],nil]  atIndex:i]; 

    [scrollView addSubview:myview]; 
    [myview release]; //<-- Fix memory leak 
} 

... 

//inside of - (void) assignImageToImageView: 
for(int i=0; i<[imgAndTagReference count] ;i++){ 
    UIImage *img = [[imgAndTagReference objectAtIndex:i] objectAtIndex:0]; 
    [(UIImageView*)[pictureImageViewArray objectAtIndex:i] setImage:img]; 
} 
+0

myview实例也在循环中泄漏。 – XJones 2011-04-12 16:50:01

+0

是的,他们是!没有真正看到代码的那部分。它看起来像用户...也不正确地使用ivars。 – Joe 2011-04-12 16:51:52

+0

非常感谢:) – pallavi 2011-04-13 05:49:30