1

I am working on an iphone app where I have to pick images from different folder after a button click. I have 5 UIButtons on UI and 5 Images folder each contains near about 100 images. I use same array for all UIButtons images. I fill array by following function :

-(void)playAnimations:(NSString*)animationName:(int)photoCount
{

NSString *photoPathName;
UIImage *object = [[UIImage alloc]init];

for (int i=1; i<photoCount+1; i++)
{
    NSString *imagesName=@"";
    if (i<10) {
        imagesName =[photoPathName stringByAppendingFormat:@"000%d",i];
    }
    else if(i>9 && i<100)
        imagesName =[photoPathName stringByAppendingFormat:@"00%d",i];
    else if(i>99 && i<1000)
        imagesName =[photoPathName stringByAppendingFormat:@"0%d",i];

    object = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imagesName ofType:@"png"]];//[UIImage imageNamed:imagesName];
    [self.arrayImagesName addObject:object];

}   
self.animationImage.animationImages = self.arrayImagesName;

self.animationImage.animationDuration =durationTime ;// defaults is number of animation images * 1/30th of a second
self.animationImage.animationRepeatCount = 1; // default is 0, which repeats indefinitely
[self.animationImage startAnimating];
}

I am getting memory leak again and again from second click(it runs proper on first click) and app gets crashes.

Where I am making mistake? please help/suggest me right way to implement it.

Thanks.

1 Answer 1

1

I think this is the problem :

UIImage *object = [[UIImage alloc]init];

you should initialize this object variable in the loop,

for (int i=1; i<photoCount+1; i++)
{
    // some code
    UIImage *object = [[UIImage alloc]initWithContentOfFile:[[NSBundle mainBundle] pathForResource:imagesName ofType:@"png"]];
    [self.arrayImagesName addObject:object];
}   

When you press the button, just use your self.arrayImagesName array, don't add same image to it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.