I'm a beginner and still trying to figure out how to read this.
There are 3 custom views and at the start I allocate the first one.
And then the second / deallocate the first and then the third / deallocate the second.
I do empty/nil all arrays right before deallocating each view so from what I see, all memory retained each time I allocated views, should be released whenever I deallocate/nil them but in the graph it keeps increasing, I don't see anything being released at all.
Is that supposed to look like that? I'm nil-ing delegates, arrays, dictionaries etc everything.
-(void)firstTOsecond {
[self.first removeFromSuperview];
self.first.delegate = nil;
self.first = nil;
self.second = [[Second alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:self.second];
self.second.delegate = self;}
-(void)secondTOthird {
[self.second removeFromSuperview];
self.second.delegate = nil;
self.second = nil;
self.third = [[Third alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:self.third];
self.third.delegate = self;}
EDIT
In First.m / Second.m / Third.m
-(instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.buttonStartFrame = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"buttonStartFrame"]];
[self addSubview:self.buttonStartFrame];
self.buttonStartFrame.userInteractionEnabled = YES;
self.buttonStartButton = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"buttonStartDefault"]];
[self.buttonStartFrame addSubview:self.buttonStartButton];
self.buttonStartButton.userInteractionEnabled = YES;
self.labelStart = [[UILabel alloc]init];
[self.buttonStartButton addSubview:self.labelStart];
self.labelStart.textColor = [UIColor darkGrayColor];
self.labelStart.text = #"Start";
}
return self;}
the memory leak could be that you are continuously creating new view elements when they could be updated instead .. you can create helper methods to avoid allocating new views, and updating the one's on the main thread instead
- (void)limitedUpdateTextColorView:(NSDictionary *)somethingCache {
self.labelStart.textColor = [somethingCache valueForKey:#"bar"];
self.labelStart.text = [somethingCache valueForKey:#"foo"];
}
Related
Good day! Is there any way on how to reduce or revert the memory used to a freshly opened app state? I'm developing a Menu app for a restaurant. On the main screen i have a buttons for Main Course,Appetizers,Soups etc. If you click on one category it will take you to another view, That is UINavigation Controller with UITableViewController, now when you click on any row of that UITableView it will take you to another ViewController which contains UIScrollView with images. This Last ViewController has this code to loop many images.
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
self.scrollView.pagingEnabled = YES;
[self.scrollView setAlwaysBounceHorizontal:NO];
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.scrollEnabled = YES;
//setup internal views
NSInteger numberOfViews = 5;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
image = [[UIImageView alloc] initWithFrame:
CGRectMake(xOrigin, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
image.image = [UIImage imageNamed:[NSString stringWithFormat:
#"main_%d", i+1]];
image.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:image];
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(updatePrices:) userInfo:nil repeats:YES];
}
//set the scroll view content size
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width *
numberOfViews,
self.view.frame.size.height);
//add the scrollview to this view
[self.view addSubview:self.scrollView];
So basically in 1 Category Lets say Main Course, i have this
http://oi60.tinypic.com/f1fr0j.jpg
Each category i have that setup of views. Now, The main problem is, when i open the app, it only consumes 6MB, then when i Tap a button, to show the Menu for the selected category, it goes up to 17mb, Now When i click anything on the Food lists, and shows the image viewcontroller, it goes up to 68mb. I tried to add a button on each to and add some actions like this
- (IBAction)backToHome:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
self.view = nil;
}
The main problem is, When i go back to the MainViewController, The consumed Memory is there, so the app gets really slow if they select more categories because it increases the memory usage. How can i Reduce or "Clear Cache" of the used memory to prevent slowing the app? Thank you!
Calling imageNamed will always cache the image in the memory to increase speed, so event if you release holder view(s) the memory usage won't drop. Use some other method to load your images(imageWithContentsOfFile or other) and make caching by yourself if you need it at all.
#property(nonatomic, strong) NSMutableDictionary* cachedImages;
-(UIImage*)imageNamedBOOM:(NSString*)aImageName
{
UIImage* result = _cachedImages[aImageName];
if( !result )
{
result = [UIImage imageWithContentsOfFile:aImageName];
_cachedImages[aImageName] = result;
}
return result;
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
[_cachedImages removeAllObjects];
}
And then in your code just call it like this(note that you have to provide image extension png, jpg, etc):
-(void) setupScrollView {
//add the scrollview to the view
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
self.scrollView.pagingEnabled = YES;
[self.scrollView setAlwaysBounceVertical:NO];
//setup internal views
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIImageView *image = [[UIImageView alloc] initWithFrame:
CGRectMake(xOrigin, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
image.image = [self imageNamedBOOM:[NSString stringWithFormat:
#"image_%d.png", i+1]];//make sure here you provide proper image extension JPG, PNG or other
image.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:image];
}
//set the scroll view content size
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width *
numberOfViews,
self.view.frame.size.height);
//add the scrollview to this view
[self.view addSubview:self.scrollView];
}
And in back to home:
- (IBAction)backToHome:(id)sender {
[_cachedImages removeAllObjects]
[self dismissViewControllerAnimated:YES completion:nil];
self.view = nil;
}
I'm trying to make my View Controllers leaner by abstracting common UI away into UIView subclasses and just instantiating them in my VCs, much like I would create HTML templates in web dev.
Here is my VC:
- (void)addEmptyView {
self.emptyHomeView = [[PLOTEmptyHomeView alloc] initWithFrame:CGRectMake(0, 10, self.view.bounds.size.width, self.view.bounds.size.height - 113) andUser:self.userModel.user];
[self.view addSubview:self.emptyHomeView];
}
And the UIView I subclass and instantiate:
- (id)initWithFrame:(CGRect)frame andUser:(NSDictionary *)user {
self = [super initWithFrame:frame];
if(self){
self.user = user;
[self drawUI];
}
return self;
}
- (void)drawUI {
self.arrowUp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"arrow-up"]];
CGRect arrowUpFrame = self.arrowUp.frame;
arrowUpFrame.origin.x = self.bounds.size.width - 60;
arrowUpFrame.size.width = 33.75;
arrowUpFrame.size.height = 33;
self.arrowUp.frame = arrowUpFrame;
[self addSubview:self.arrowUp];
self.welcome = [[UILabel alloc] initWithFrame:CGRectMake(0, 167.5, self.bounds.size.width, 22.5)];
NSArray *nameParts = [self.user[#"name"] componentsSeparatedByString:#" "];
self.welcome.text = [NSString stringWithFormat:#"Hey %#", nameParts[0]];
self.welcome.font = [UIFont fontWithName:#"Avenir-Light" size:24];
self.welcome.textColor = [UIColor plotPlaceholderGrey];
self.welcome.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.welcome];
}
Now this code all works fine, but for my own understanding, am I abusing the init method? I'm also aware of layoutSubviews & drawRect in UIView but i'm not sure if I should be using them in the above scenario?
Any pointers are appreciated...
Your not abusing of init method, but you have to declare all your ui objects and all their properties without define the frame and in the layoutsubviews, you define the frame of all your object. It's my way to declare an custom view.
- (id)initWithFrame:(CGRect)frame andUser:(NSDictionary *)user {
self = [super initWithFrame:frame];
if(self){
self.user = user;
self.arrowUp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"arrow-up"]];
[self addSubview:self.arrowUp];
self.welcome = [[UILabel alloc] init];
NSArray *nameParts = [self.user[#"name"] componentsSeparatedByString:#" "];
self.welcome.text = [NSString stringWithFormat:#"Hey %#", nameParts[0]];
self.welcome.font = [UIFont fontWithName:#"Avenir-Light" size:24];
self.welcome.textColor = [UIColor plotPlaceholderGrey];
self.welcome.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.welcome];
}
return self;
}
- (void)layoutSubviews {
CGRect arrowUpFrame = self.arrowUp.frame;
arrowUpFrame.origin.x = self.bounds.size.width - 60;
arrowUpFrame.size.width = 33.75;
arrowUpFrame.size.height = 33;
self.arrowUp.frame = arrowUpFrame;
self.welcome.frame =CGRectMake(0, 167.5, self.bounds.size.width, 22.5)];
}
Hope it will help you.
Optimally, you can instantiate and add subviews in your method as shown, but do actual layout involving frames in layoutSubviews. The code you have will work as is, but if your initialize would be from a nib or something that doesn't provide the core frame (or autolayout was involved) you may not truly know the proper bounds/frame at init time. Often layout code is dependent on other factors that may change so layoutSubviews is a more proper location.
I'm working on an iPad app and at some points, I need to show a popover with options for a user to pick from. For this, I use a UITableView in a UIPopoverController. The problem is that, on an iPad (not on the simulator), when scrolling the tableview, I get a sort of "double vision" effect, where it appears like two sets of of the list exist. One that is stationary, and one that scrolls up and down.
I construct the popover like this:
self.fClassTypeList = [[NSMutableArray alloc] init];
[self.fClassTypeList removeAllObjects];
NSUInteger stringLength = 0;
(populate self.fClassTypeList, and set stringLength to the size of the longest entry)
[self setContentSizeForViewInPopover:CGSizeMake(stringLength * 15.0f, [self.fClassTypeList count] * 30)];
CGFloat tableBorderLeft = 5;
CGFloat tableBorderRight = 5;
CGRect viewFrame = self.view.frame;
viewFrame.size.width -= tableBorderLeft + tableBorderRight; // reduce the width of the table
self.fListOfItems = [[UITableView alloc] initWithFrame:viewFrame style:UITableViewStylePlain];
self.fListOfItems.delegate = self;
self.fListOfItems.dataSource = self;
[self.view addSubview:self.fListOfItems];
I put in the viewDidLayoutSubviews(…) part of the view controller, maybe I should put it somewhere else? I am not sure why this happens on the actual machine, but not the simulator.
-viewDidLayoutSubviews is a weird place to put allocations because that method can be called multiple times. So as far as your main issue goes, I believe you should move your allocations into the -init method, and move your layout code into your -viewWillAppear method.
- (id)init
{
self = [super init];
if (self)
{
self.fClassTypeList = [[NSMutableArray alloc] init];
self.fListOfItems = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
self.fListOfItems.delegate = self;
self.fListOfItems.dataSource = self;
[self.view addSubview:self.fListOfItems];
}
return self;
}
- (void )viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSUInteger stringLength = 0;
CGFloat tableBorderLeft = 5;
CGFloat tableBorderRight = 5;
CGRect viewFrame = self.view.frame;
viewFrame.size.width -= tableBorderLeft + tableBorderRight; // reduce the width of the table
self.fListOfItems.frame = viewFrame;
[self setContentSizeForViewInPopover:CGSizeMake(stringLength * 15.0f, [self.fClassTypeList count] * 30)];
}
This promotes better memory management.
As an added bonus, I would recommend you refactor the
[self setContentSizeForViewInPopover:CGSizeMake(stringLength * 15.0f, [self.fClassTypeList count] * 30)]; method into a setter method of fClassTypeList. Even better is to simply call -viewWillAppear: in that same setter method instead. This will promote good scalability as you (or someone else) continues to build upon this code later on.
It's a little confusing to see what exactly you're trying to accomplish in this code because it's so hardcoded so let me know if I'm missing the mark you're looking for (w/ an explanation why) and I'll make an edit.
Cheers
Does the following code (removeViews) correctly remove the reference to the objects, i.e delete them, so I do not keep making more Views when the method createViews is called. createViews creates the views and removeViews sets them to nil. Note: this is a very simple example to enhance understanding, and serves no actual purpose.
-(void) createViews{
UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,200,200)];
tableView.delegate=self;
tableView.datasource = self;
self.mainTableView = tableView;//self.mainTableView is a weak reference
[self.view.superView addSubview: self.mainTableView];
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,400)];
view.backgroundColor = [UIColor redColor];
self.mainView = view;//self.mainView is a strong reference
[self.view.superView addSubviews:self.mainView];
}
-(void) removeViews{
self.mainView = nil;
self.mainTableView=nil;
}
First remove them from their superView and then set them to nil
-(void) removeViews{
[self.mainView removeFromSuperview];
[self.mainTableView removeFromSuperview];
self.mainView = nil;
self.mainTableView=nil;
}
I've used UITapGestureRecognizer tons of times, but in this case I'm getting EXC_BAD_ACCESS when a tap occurs. I think it has to do with the fact that I'm adding it to an Alert-type overlay view. And for some reason the overlay view isn't getting retained even though it's on-screen.
I'm creating the view like this:
HelperView *testHelper = [HelperView helperWithBodyText:#"test text"];
[testHelper presentAtPoint:screenCenter];
The convenience method in HelperView.m looks like this:
+ (id)helperWithBodyText:(NSString*)text
{
return [[self alloc] initWithFrame:CGRectZero bodyText:text];
}
And the rest of the code looks like this:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.container = [[AGWindowView alloc] initAndAddToKeyWindow];
self.container.supportedInterfaceOrientations = UIInterfaceOrientationMaskLandscapeRight;
self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
self.overlay.backgroundColor = [UIColor redColor];
self.overlay.alpha = 0.6;
[self.container addSubviewAndFillBounds:self.overlay]; //this fills the screen with a transparent red color, for testing
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissMe:)];
[self.overlay addGestureRecognizer:tap]; //I want to dismiss the whole view if the overlay is tapped
self.content = [[UIView alloc] initWithFrame:CGRectZero];
}
return self;
}
- (id)initWithFrame:(CGRect)frame bodyText:(NSString*)bodyText
{
self = [self initWithFrame:frame];
if (self) {
//TEST frame
self.content.frame = CGRectMake(0, 0, 217, 134);
// Initialization code
UIImage *bgImage = [[UIImage imageNamed:#"helper-bg"]
resizableImageWithCapInsets:UIEdgeInsetsMake(30, 28, 20, 20)];
UIImageView *bgImgView = [[UIImageView alloc] initWithImage:bgImage];
bgImgView.bounds = self.content.frame;
[self.content addSubview:bgImgView];
}
return self;
}
- (void)presentAtPoint:(CGPoint)loc
{
CGPoint newPoint = [self.container convertPoint:loc toView:self.container];
self.content.center = newPoint;
[self.container addSubview:self.content];
}
- (void)dismissMe:(UITapGestureRecognizer*)recognizer
{
//this never happens - I get EXC_BAD_ACCESS when I tap the overlay
}
HelperView is not the view getting displayed and it's not getting retained, but you're using as the target for the gesture recognizer. The AGWindowView property "container" is getting displayed and retained by its superview. Your code needs refactoring since you have this view HelperView that doesn't ever display anything itself, but if you want it to work like this you need to retain HelperView so it doesn't automatically get released. You can do this by assigning it to a strong instance variable.