iphone - performSelectorInBackground图像加载

时间:2011-04-12 13:15:57

标签: iphone uiscrollview

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

@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;


}

1 个答案:

答案 0 :(得分:0)

在以下行中,您将创建内存泄漏

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

您不需要分配和初始化新的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];
}