简体   繁体   中英

App getting crash in background image loading iphone

I have scroll view with 60 UIImageView's . Images displaying in these imageviews are from url and url's i get from the webservice. When user scrolls to bottom I call the webservice and get new 60 urls . After getting the url i am utilizing same UIImageView's to display images. Following is my code for displaying images.

for (UIView *viewSel in [scrlView subviews]) {
    NSString *strImgUrl = [arrImgURL valueForKey:@"url"];
    if ([viewSel isKindOfClass:[UIImageView class]]) {

            UIImageView *imgView = (UIImageView *)viewSel;

            [imgView setImage:[UIImage imageNamed:@"loading432x520.png"]];

            NSMutableArray *arr = [[NSMutableArray alloc] init];

            [arr addObject:[NSString stringWithFormat:@"%@",strImgUrl]];

            [arr addObject:imgView];
            [self performSelectorInBackground:@selector(loadImageInBackground:) withObject:arr];

            [arr release];
    }
}


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

    NSLog(@"loadImage");
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[arr objectAtIndex:0]]];
    UIImage *img    = [[UIImage alloc] initWithData:imgData];
    if (img != nil) {
        [arr addObject:img];
    }
    else{
        [arr addObject:[UIImage imageNamed:@"no-image432x520.png"]];
    }
    [img release];
    [self performSelectorOnMainThread:@selector(assignImageToImageView:) withObject:arr waitUntilDone:YES];
    [pool release];
}
- (void) assignImageToImageView:(NSMutableArray *)arr{
    NSLog(@"assignImage");
    UIImageView *imgView = [arr objectAtIndex:1];
    imgView.image = [arr objectAtIndex:2];
}

The code works perfect for first time. But when i get new urls it is working some time or getting crash. I don't know why it is getting crash. I want to stop that crash. If you are not getting to my question then let me know. Please help me for this. Thanks in advance. Valid answer will be appreciated.

Thank you all to give your help full comments to my question. I got the error. I am adding CALayer to uiimageview when allocating memory to uiimageview. I remove that CALayer code and it work perfectly. Nikita's comment help me to find my error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM