fix the memory issue warning when leaks in unable to find it

285 Views Asked by At

I am unsing the below code in a view controller.. I am fixing the memory issue problem. when I comment out the view did load even it takes a lot of memory above 80 MB what shuold I do to solve it. I changed all variable to strong to weak.

@interface bookNowViewController :     UIViewController<GMSMapViewDelegate, UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionViewBook;
@property (weak, nonatomic) IBOutlet GMSMapView *mapLoc;
@property (weak, nonatomic) IBOutlet UILabel *distanceLbl;
@property (weak, nonatomic) IBOutlet UILabel *rateLbl;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionViewFacility;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIScrollView *mainScrollView;
@property (weak, nonatomic) IBOutlet UIImageView *animatedImages;//images with fading effect
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *facilityCollectionViewHight;
@property (weak, nonatomic) IBOutlet UILabel *noSportsAvailable;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *facilityLblHightConstant;
@property (weak, nonatomic) IBOutlet UILabel *stName;
@property (weak, nonatomic) IBOutlet UILabel *stAddress;
@property (weak, nonatomic) IBOutlet UIButton *btnI;
@property (weak, nonatomic) IBOutlet UIButton *moreBt;
@property (weak, nonatomic) IBOutlet UIButton *bookNowBt;
@property (weak, nonatomic) IBOutlet UILabel *spRating;
@property (weak, nonatomic) IBOutlet UIView *iNewView;
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
@property (weak, nonatomic) IBOutlet UILabel *lblIText;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UILabel *confirmationLblText; 

and in .m file

@interface bookNowViewController (){
NSMutableArray *savedList, *stadiumAddress, *stadiumLatitude, *stadiumLongitude, *stadiumId, *stadiumImages, *stadiumPrice, *stadiumRating, *stadiumName, *savedImages, *listFacility, *facility, *vendorId, *vendorName, *sportsName;

NSString *post_id2, *postIndex2, *combineData2, *STName;
NSString *raterName, *ratingValue, *raterImage, *comment;
NSInteger myInt;
UIButton *button;
UILabel *navLabel;
NSString *stadium_Id;
NSMutableArray *imageView;// animation images array
UIView  *hideView;
UITapGestureRecognizer *oneTap;
 NSUInteger jk;

}

  @end

and in viewdidload

 [NSTimer scheduledTimerWithTimeInterval:5.0 target:self
                               selector:@selector(swapImage2) userInfo:nil repeats:YES];

 [[NSUserDefaults standardUserDefaults] setObject: nil forKey: @"paymentTag"];
self.navigationController.navigationBar.hidden = NO;

stadium_Id = [[NSUserDefaults standardUserDefaults] valueForKey:@"stadium_Id"];
NSLog(@"%@", stadium_Id);
[ProgressHUD showSuccess:@"" Interaction:YES];
NSString *combined2 = stadium_Id;
NSArray* combineData = [[NSArray alloc]init];
combineData = [combined2 componentsSeparatedByString: @" "];
post_id2 = [combineData objectAtIndex:0];
[[NSUserDefaults standardUserDefaults] setObject: post_id2 forKey: @"stadiumid"];
NSLog(@"%@", post_id2);
postIndex2 = [ combineData objectAtIndex:1];
STName =[combineData objectAtIndex:2];
myInt = [postIndex2 integerValue];
NSLog(@"%ld", (long)myInt);

[self getStadiumDetail];
[self map2];

  savedImages = [[NSUserDefaults standardUserDefaults] valueForKey:@"sportsImage"];
  sportsName = [[NSUserDefaults standardUserDefaults] valueForKey:@"sportsName"];

if (savedImages.count<1) {
    _infoLabel.hidden = NO;
    _bookNowBt.hidden = YES;
}
else{
   _infoLabel.hidden = YES;
}

[_collectionViewBook reloadData];
[_collectionViewFacility reloadData];

//////////tap gesture
oneTap.numberOfTapsRequired=1;
oneTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap332)];
oneTap.delegate = self;

// [ProgressHUD showSuccess:@"" Interaction:YES];
self.btnI.layer.cornerRadius = self.btnI.frame.size.width/2;
[self.btnI.layer setBorderWidth:2.0];
[self.btnI.layer setBorderColor:[[UIColor grayColor] CGColor]];


_moreBt.layer.cornerRadius = 4.0f;
_bookNowBt.layer.cornerRadius = 4.0f;

_rateLbl.layer.borderColor = [UIColor lightGrayColor].CGColor;
_rateLbl.layer.borderWidth = 2.0f;
_rateLbl.layer.cornerRadius =_rateLbl.frame.size.width/2;
_rateLbl.clipsToBounds = YES;


_confirmationLblText.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"savedPayText"];
1

There are 1 best solutions below

1
Anurag Sharma On
  1. In the very end ,Nil all the Images you have declared previously.

  2. After every pushtoViewController nil the reference you created for it.

  3. Use @autoreleasepool.

  4. keep weak references for Outlets and assign any strong references to nil** inviewWillDisapper: . You can present a viewController modally as a temporary interruption to obtain important information from the user.

  5. Instead of creating new MKMapView's as needed in the app, I added an mkMapView property to my AppDelegate and only created it when needed. Once it has been created, it lives in the AppDelegate forever and I reuse that single instance everywhere needed. This really helped in reducing the amount of memory being used as I was previously instantiating a couple different MKMapView's and both were burning through memory pretty quickly.

  6. Don’t use -imageNamed i’m saying it again don’t use imageNamed ever method because it caches the images. Use this instead of -imageNamed : (make a method on appDelegate and then call it anywhere you want)

-(UIImage *)method4TakeImagesFromBundel:(NSString *)nameOfImage { NSString fileLocation = [[NSBundle mainBundle] pathForResource:nameOftheImage ofType:@"png"]; UIImage yourImage = [[UIImage alloc] initWithContentsOfFile:fileLocation]; return yourImage; }

  1. After getting the image compress it and use it:

NSData *imgData= UIImageJPEGRepresentation(rainyImage,0.1 /compressionQuality/);

  1. You can further resize it:
  • (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size { UIGraphicsBeginImageContext(size); [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext(); return destImage; }