开发者

how do i scroll through 100 photos in UIScrollView in IPhone

I'm trying to scroll through images being downloaded from a users online album (like in the facebook iphone app) since i can't load all images into memory, i'm loading 3 at a time (prev,current & next). then removing image(prev-1) & image (next +1) from the uiscroller subviews. my logic works fine in the simulator but fails in the device with this error: [CALayer retain]: message sent to deallocated instance what could be the problem below is my code sample

- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView 
{
    pageControlIsChangingPage = NO;
    CGFloat pageWidth = _scrollView.frame.size.width;
    int page = floor((_scrollView.contentOf开发者_如何学运维fset.x - pageWidth / 2) / pageWidth) + 1;

    if (page>1 && page<=(pageControl.numberOfPages-3)) {
        [self removeThisView:(page-2)];
        [self removeThisView:(page+2)];
    }

    if (page>0) {
        NSLog(@"<< PREVIOUS");
        [self showPhoto:(page-1)];
    }

    [self showPhoto:page];
    if (page<(pageControl.numberOfPages-1)) {
        //NSLog(@"NEXT >>");
        [self showPhoto:page+1];
        NSLog(@"FINISHED LOADING NEXT >>");
    }
}

-(void) showPhoto:(NSInteger)index
{
    CGFloat cx = scrollView.frame.size.width*index;
    CGFloat cy = 40;

    CGRect rect=CGRectMake( 0, 0,320, 480);
    rect.origin.x =  cx;
    rect.origin.y = cy;
    AsyncImageView* asyncImage = [[AsyncImageView alloc] initWithFrame:rect];
    asyncImage.tag = 999;
    NSURL *url = [NSURL URLWithString:[pics objectAtIndex:index]];

    [asyncImage loadImageFromURL:url place:CGRectMake(150, 190, 30, 30) member:memberid isSlide:@"Yes" picId:[picsIds objectAtIndex:index]];

    [scrollView addSubview:asyncImage];
    [asyncImage release];
}

- (void) removeThisView:(NSInteger)index
{
    if (index<[[scrollView subviews] count] && [[scrollView subviews] objectAtIndex:index]!=nil) {
        if ([[[scrollView subviews] objectAtIndex:index] isKindOfClass:[AsyncImageView class]] || [[[scrollView subviews] objectAtIndex:index] isKindOfClass:[UIImageView class]]) {
            [[[scrollView subviews] objectAtIndex:index] removeFromSuperview];
        }
    }
}

For the record it works OK in the simulator, but not the iphone device itself. any ideas will be appreciated. cheers, fred.


I guess your AsyncImageView class uses asynchronous connections to load the image (which is a good thing), so that means that when you add it as a subview of your scroll view, it might be "removedFromSuperview" before the image loads completely. Check in the implementation of your class if the delegates are properly nil'd when it's been dealloc'd, and also that any connections are cancelled and so on.

Pay attention to the fact that a class should never retain its delegate, an "assign" kind of property is perfect for these situations.


Some tips:

  1. Instead of alloc/init'ing and release'ing so many instances while you scroll, try to keep three ivars in your class, and just change the "frame" property of the instances as you scroll. You will avoid lots of allocation and releases if the user scrolls fast, and that's a good thing on an iPhone.
  2. Avoid direct ivar accesses like that in your code; use properties, there are many advantages to them beyond the simple "correctness" thing (KVO being one of them).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜