I am trying to make a simples application where the user can go through notes and then do a quiz. I am presenting this notes as images and the image would be assigned to an UIImageView
to be shown to the user.
In my view did load I am mostly setting the UI. Then I would be creating my array containing my array of images (notes)
- (void)viewDidLoad {
UIColor *colourForNavigationBar = [self colorWithHexString:@"919D9F"];
[[UINavigationBar appearance] setBarTintColor:colourForNavigationBar];
UIColor *colourForBackground = [self colorWithHexString:@"F0F3F4"];
self.view.backgroundColor = colourForBackground;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:27.0];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
self.navigationItem.titleView = label;
label.text = @"Notes";
[label sizeToFit];
UIColor *colourForButton = [self colorWithHexString:@"919D9F"];
self.previousButton.backgroundColor = colourForButton;
self.nextButton.backgroundColor = colourForButton;
finishedNotes = NO;
playerPlaying = NO;
arrayOfImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"],
[UIImage imageNamed:@"10.png"],
[UIImage imageNamed:@"11.png"],
[UIImage imageNamed:@"12.png"],
[UIImage imageNamed:@"13.png"],
nil];
}
Then there is the 'next' button which allows the user to continue on to the next note. Below is the code for the action
- (IBAction)nextNote:(id)sender {
if (finishedNotes == YES) {
[self performSegueWithIdentifier:@"quizFromNotes" sender:self];
}
self.previousButton.enabled = YES;
stateOfPhoto++;
if (stateOfPhoto < 13) {
UIImage *image = [arrayOfImages objectAtIndex:stateOfPhoto-1];
self.imageView.image = image;
}
if (stateOfPhoto == 13) {
UIImage *image = [arrayOfImages objectAtIndex:stateOfPhoto-1];
self.imageView.image = image;
[self.nextButton setTitle:@"Begin Quiz" forState:UIControlStateNormal];
finishedNotes =YES;
}
if (playerPlaying == YES) {
[self.notesPlayer stop];
playerPlaying = NO;
}
}
Everything is working fine and smoothly. However whenever there is the change of image, the memory used by the application increasing by a few megabytes. Below is the photo of the memory usage.
From the black line onwards, when I press the 'next' button, the memory used increases. It just repeats itself until the user has continued onto the quiz. If I end the quiz and start over the notes, the memory usage would continue to rise from the previous memory usage (73 mb). Eventually, the app would crash and I cannot launch the app anymore.
I have never faced memory leakage issues before and hence, I am not sure of what to do. I have tried googling for the past hour and am not able to come up with a solution to this. So my question is how would I be able to release the memory or reduce the memory usage.
I think it's because of your images, how big are those images you're showing?
Based on this answer: https://stackoverflow.com/a/22662754/3231194 , you can either use this method to scale your images before putting them into your UIImageView
:
-(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
But that will take up a CPU usage (I'm not sure though if it is a big deal).
OR
You can just resize your images by yourself before importing them to your Xcode project.
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.