Memory Leak issue in UIPage Control

224 views Asked by At
- (void)loadScrollViewWithPage:(int)page
{
    if (page < 0)
    return;
    if (page > kNumberOfPages)
    return;

if([searchResultArray count]>6)
{
    if([searchResultArray count]%6==0)
    {
        kNumberOfPages = [searchResultArray count]/6;
    }
    else {
        kNumberOfPages = [searchResultArray count]/6;
        kNumberOfPages = kNumberOfPages +1;
    }

    pageControl.numberOfPages = kNumberOfPages;
    ScrollView.contentSize = CGSizeMake(ScrollView.frame.size.width *kNumberOfPages, ScrollView.frame.size.height);
}
else {
    kNumberOfPages = 1;
    pageControl.numberOfPages = kNumberOfPages;
}

int z=-1;
int a = 0;

for(UIView *subview in [ScrollView subviews]) 
{
    [subview removeFromSuperview];
}

for(int x = 0; x < [searchResultArray count];x++)
{   
    if(x>=6 && x%6==0)
    {
        z=0;
        a = a+320;
    }
    else {
        z++;
        //a++;
    }

    UIButton *ProfileImageButton = [[UIButton alloc] initWithFrame:CGRectMake(a+8+(100*(x%3))+(x%3)*3,15+(110*(z/3))+(z/3)*10, 100, 85)];
    [ProfileImageButton setBackgroundColor:[UIColor clearColor]];
    [ProfileImageButton setClearsContextBeforeDrawing:YES];
    [ProfileImageButton setTag:x];
    [ProfileImageButton setUserInteractionEnabled:YES];
    NSString *ImagePath =[NSString stringWithFormat:@"%@",[[searchResultArray objectAtIndex:x]valueForKey:@"Photo"]];
    [ProfileImageButton setImageWithURL:[NSURL URLWithString:ImagePath]
                           placeholderImage:[UIImage imageNamed:@"[email protected]"]];
    [ProfileImageButton addTarget:self action:@selector(profileImageClicked:) forControlEvents:UIControlEventTouchUpInside];
    [ScrollView addSubview:ProfileImageButton];
    [ProfileImageButton release];
    ProfileImageButton = nil; 

    UILabel *groupNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(a+8+(100*(x%3))+(x%3)*3,100+(110*(z/3))+(z/3)*10, 100, 25)];
    [groupNameLabel setBackgroundColor:[UIColor clearColor]];
    [groupNameLabel setText:[NSString stringWithFormat:@"%@,%@,%@",[[searchResultArray objectAtIndex:x] valueForKey:@"Username"],[[searchResultArray objectAtIndex:x] valueForKey:@"Age"],[[searchResultArray objectAtIndex:x] valueForKey:@"Gender"]]];
    [groupNameLabel setUserInteractionEnabled:YES];
    [groupNameLabel setFont:[UIFont systemFontOfSize:15.0f]];
    [groupNameLabel setTextColor:[UIColor whiteColor]];
    [ScrollView addSubview:groupNameLabel];
    [groupNameLabel release];
    groupNameLabel = nil;
}   

}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
 if (pageControlUsed)
    {
    return;
}

CGFloat pageWidth = ScrollView.frame.size.width;
int page = floor((ScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;


[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
pageControlUsed = NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
pageControlUsed = NO;
}

- (void)changePage:(id)sender
{
int page = pageControl.currentPage;

[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];

// update the scroll view to the appropriate page
CGRect frame = ScrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[ScrollView scrollRectToVisible:frame animated:YES];

pageControlUsed = YES;
}

I am working on an app in which I am retrieving images from server and those images are to be shown on UIPageControl, though I have done this.BUT on on scrolling pagecontrol many times it shows memory leak warning and app exits

ON CONSOLE

Received memory warning. Program received signal: “0”. Data Formatters temporarily unavailable, will re-try after a 'continue'. (Unknown error loading shared library "/Developer/usr/lib/libXcodeDebuggerSupport.dylib")

0

There are 0 answers