Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7848839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:20:52+00:00 2026-06-02T18:20:52+00:00

My app crashes when I set the image-property on a UIImageView three times in

  • 0

My app crashes when I set the image-property on a UIImageView three times in a row (sometimes two times is enough). A few times I’ve seen a memory warning before the app closes, but most of the times it just collapses. The app does not crash in the simulator, so I’m quite sure it is a memory problem.

Here’s the code I use when setting the image property:

-(void)changeBgPictureTo:(UIImage *)img
{
    [self.backgroundImage setImage:img];
}

The UIImages is allocated with the [UIImage imageWithData:] method:

[UIImage imageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:type]]];

The image is set correctly the first two times, but crashes on the third time. It has nothing to do with a specific image file. I’ve tried with 10 different images and it makes no difference.

How do I make the UIImageView unload previous loaded images?

EDIT:
Okay, I’ve been asked for the whole code so here it goes:

I’m working with a class which looks like this:

@interface MyImage : NSObject
{
    UIImage* image;
    int imgId;
    NSArray* colors; //contains 'UIColor' objects.
}

@property (nonatomic, retain) UIImage* image;
@property (nonatomic, retain) NSArray* colors;
@property (nonatomic, readwrite) int imgId;

-(id)initWithFileName:(NSString*)fileName withType:(NSString*)type andId:(int)imgId andColors:(NSArray*)colorArray;

@end

Here is the init implementation:

-(id)initWithFileName:(NSString*)fileName withType:(NSString*)type andId:(int)imageId andColors:(NSArray*)colorArray
{
     self = [super init];

     if (self)
     {
         self.colors = [NSArray arrayWithArray:colorArray]; 
         self.imgId = imageId;
         self.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:type]]];
     }   

return self;

}

I have a datacontroller which has a list of ‘MyImage’ objects which is added in a loop with this call:

[self.myImages addObject:[[MyImage alloc] initWithFileName:[NSString stringWithFormat:@"0000%d", i] withType:@"jpg" andId:i andColors:colors]];

I have 9 images which is named 00001.jpg, 00002.jpg, ….., 00008.jpg.

This data controller has a method like this:

-(MyImage *)getImageWithId:(int)imgId
{
    for (MyImage* img in self.myImages)
    {
        if (img.imgId == imgId)
            return img;
    }
    return nil;
}

The getImageWithId method is called like this:

-(void)btnPushed:(id)sender
{
     [self.delegate changeBgPictureTo:[self.imgDataController getImageWithId:((UIButton*)sender).tag]];    
}  

The changeBgPictureTo method is the method which makes the setting of the image-property:

-(void)changeBgPictureTo:(MyImage *)img
{
    NSLog(@"Setting image: %d", img.imgId);
    [self.backgroundImage setImage:img.image];    
}

The log prints “Setting image: 0000X:” three times, but crashes shortly after the third print.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:20:53+00:00Added an answer on June 2, 2026 at 6:20 pm

    Instead of storing the UIImage object inside MyObject class,try storing only the filename of the file.

    @interface MyImage : NSObject 
    {
        NSString *fileName;//UIImage* image;
        int imgId;
        NSArray* colors; //contains 'UIColor' objects.
    }
    
    @property (nonatomic, retain) NSString *fileName;//@property (nonatomic, retain) UIImage* image;
    @property (nonatomic, retain) NSArray* colors;
    @property (nonatomic, readwrite) int imgId;
    
    -(id)initWithFileName:(NSString*)fileName withType:(NSString*)type andId:(int)imgId andColors:(NSArray*)colorArray;
    

    and implementation

    -(id)initWithFileName:(NSString*)fileName withType:(NSString*)type andId:(int)imageId   andColors:(NSArray*)colorArray
    {
         self = [super init];
    
         if (self)
         {
             self.colors = [NSArray arrayWithArray:colorArray]; 
             self.imgId = imageId;
             //self.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:type]]];
             self.fileName = [[NSBundle mainBundle] pathForResource:fileName ofType:type]];
          }   
          return self;
     }
    

    then in function

     -(void)changeBgPictureTo:(MyImage *)img   
     {
        NSLog(@"Setting image: %d", img.imgId);
        //[self.backgroundImage setImage:img.image];    
        UIImage *img = [UIImage imageWithContentsOfFile:[img fileName]];
        [self.backgroundImage setImage:img];
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my app crashes when the user tries to delete a row from the UITableView
my app crashes often in this for-loop: for (int a = 0; a <=
my iphone app crashes unexpectedly and looking at the crash log I can't tell
When my app crashes, I get a crash report that takes 5 minutes to
My rails app crashes as soon as I access a web page. My versions
I am using getch() and my app crashes instantly. Including when doing: int main()
Does a destructor get called if the app crashes? If it's an unhandled exception
I've started using the CAGradientLayer but my app crashes CAGradientLayer *gradient = [CAGradientLayer layer];
When trying to authenticate with facebook, my app crashes. It reaches the line new
I am programming my first iPhone app (which crashes with an EXC BAD ACCESS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.