Xcode allocation problems with UIImagaView keep growing - objective-c

I load alot of images in my image gallery and in my thumbnail gallery, every time i press a button (section) i replace the images with the right seciont's images, all ok since the 6th-7th time i do this my app crash.
So i tried build with allocation tool and i see allocation memory growing up every time i press the button! i've been aspected to see a stack memory allocation not a growing one with
initWithContentOfFile.
What am i doing wrong?
for (UIView *view in [scroller subviews]) {
[view removeFromSuperview];
}
for (UIView *view in [thumbnail subviews]) {
[view removeFromSuperview];
}
for (int i=1; i<numeroimg+1; i++) {
UIImageView *imagen = [[[UIImageView alloc] initWithImage:[[UIImage alloc] initWithContentsOfFile:
[[NSBundle mainBundle]
pathForResource:[NSString stringWithFormat:#"%#%d",sezione,i]
ofType:#"jpg"]]]autorelease];
imagen.frame = CGRectMake((i-1)*1024, 0, 1024, 704);
[scroller addSubview:imagen];
UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake((i-1)*210, 30, 200, 150)]autorelease];
[button setImage:[[UIImage alloc] initWithContentsOfFile:
[[NSBundle mainBundle]
pathForResource:[NSString stringWithFormat:#"%#%d",sezione,i]
ofType:#"jpg"]] forState:UIControlStateNormal];
[button addTarget:self action:#selector(pagina:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[thumbnail addSubview:button];
}

What am i doing wrong?
You're using +alloc to create an image:
UIImageView *imagen = [[[UIImageView alloc] initWithImage:...
but you never release imagen.

Try to manually drain the autorelease pool
#autoreleasepool{
UIImageView *imagen = [[[UIImageView alloc] initWithImage:[[UIImage alloc] initWithContentsOfFile:
[[NSBundle mainBundle]
pathForResource:[NSString stringWithFormat:#"%#%d",sezione,i]
ofType:#"jpg"]]]autorelease];
imagen.frame = CGRectMake((i-1)*1024, 0, 1024, 704);
[scroller addSubview:imagen];
UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake((i-1)*210, 30, 200, 150)]autorelease];
[button setImage:[[UIImage alloc] initWithContentsOfFile:
[[NSBundle mainBundle]
pathForResource:[NSString stringWithFormat:#"%#%d",sezione,i]
ofType:#"jpg"]] forState:UIControlStateNormal];
[button addTarget:self action:#selector(pagina:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[thumbnail addSubview:button];
}

Related

Jssocials not working in UIWebView/WKWebView

I want to use jssocial plugin in iOS,there is no response when user clicks on buttons be it a Facebook, twitter or Instagram from UIWebView and also not from WKWebView so, if anybody have implemented it in iOS then please guide me about it.
Any help will be appreciable. Thank You
Set WKWebView properties and delegates as follow
WKPreferences *prefs = [WKPreferences new];
prefs.javaScriptEnabled = YES;
prefs.javaScriptCanOpenWindowsAutomatically = YES;
WKWebViewConfiguration *theConfig = [[WKWebViewConfiguration alloc] init];
theConfig.preferences = prefs;
webView = [[EasyJSWKWebView alloc] initWithFrame:self.view.bounds
configuration:theConfig
withJavascriptInterfaces:interfaces];
webView.UIDelegate = self;
webView.navigationDelegate = self;
[webView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
if (#available(iOS 11.0, *)) {
webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
[webView loadRequest:requestObj];
[self.view addSubview:webView];
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures: (WKWindowFeatures *)windowFeatures
{
ViewPopUp = [[UIView alloc] initWithFrame:self.view.frame];
[ViewPopUp setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:ViewPopUp];
UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 50, 50)];
[btnBack setImage:[UIImage imageNamed:#"back"] forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[ViewPopUp addSubview:btnBack];
PopUpView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64) configuration:configuration];
PopUpView.allowsBackForwardNavigationGestures = YES;
PopUpView.navigationDelegate = self;
[self.view addSubview:PopUpView];
return PopUpView;
}

Unable to load an image into NSImageView

I wrote the following and no image comes up. I displayed the pointer and it comes out as "0".
In the next code block I checked the path and made shore the image was present there. Still nothing loads.
NSImageView *vi = [[NSImageView alloc] initWithFrame:CGRectMake(10, 10, 220, 220)];
[self.view addSubview:vi];
NSImage *dog = [NSImage imageNamed:#"airnow3.jpg"];
NSLog(#"pointer %d", dog);
[vi setImage:dog];
NSFileManager *filemgr;
NSString *currentpath;
filemgr = [[NSFileManager alloc] init];
currentpath = [filemgr currentDirectoryPath];
NSLog(#"%#", currentpath);
Why don't you use InitWithImage:(NSImage) instead of InitWithFrame
Maybe you should use UIImageView.
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(10,10,220,220)];
UIImage *image = [UIImage imageNamed:#"airnow3.jpg"];
[imageView setImage: image];
[self.view addSubview: imageView];

Content in Table cell of iOS 8 is not display

Everything works well in iOS 9.
But in iOS 8, the content is not displayed, only imageview is showed.
What i did is that I create a view and add label and UIImage to that view. Then that view is added to contentView of table Cell
create View
- (UIView *)getHeaderView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, HEADER_SECTION_HEIGHT)];
[view setBackgroundColor:[[RNThemeManager sharedManager] colorWithHexString:[_themeComponent valueForKey:#"Layer1Color"]]];
UILabel *taskName = [[UILabel alloc] initWithFrame:CGRectMake(5.0, SECTION_BREAK+3.0, view.frame.size.width-23.0, 20)];
[taskName setFont:[UIFont boldSystemFontOfSize:[[_themeComponent valueForKey:#"RegularFontSize"] intValue]]];
[taskName setText:combinedTask.taskDescription];
[taskName setTextColor: [[RNThemeManager sharedManager] colorWithHexString:[_themeComponent valueForKey:#"textPrimary"]]];
[view addSubview:taskName];
UIImageView *addressView = [[UIImageView alloc]initWithFrame:CGRectMake(5.0, SECTION_BREAK+28.0, 20, 20)];
addressView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#.png", #"Attr_Pin"]];
[view addSubview:addressView];
UILabel *address = [[UILabel alloc] initWithFrame:CGRectMake(35.0, SECTION_BREAK+23.0, view.frame.size.width-35.0, 30)];
[address setFont:[UIFont boldSystemFontOfSize:[[_themeComponent valueForKey:#"SmallFontSize"] intValue]]];
address.lineBreakMode = NSLineBreakByWordWrapping;
address.numberOfLines = 0;
[address setText:[NSString stringWithFormat:#"%#", unit]];
[address setTextColor: [[RNThemeManager sharedManager] colorWithHexString:[_themeComponent valueForKey:#"textPrimary"]]];
[view addSubview:address];
return view;
}
Add view to table cell
UIView *header= [self getHeaderView:tableView viewForHeaderInSection:section];
cell.taskIdLabel.hidden = YES;
[cell.contentView addSubview:header];
Anyone has any ideas? Thanks much for your help.
This is because iOS 9 Apple handle good autolayout than before.
Let add [cell layoutIfNeeded] before return cell.
Update
I have same problem before. Here is block of code that i use to solved my problem.
- (void) sizeHeaderToFit {
//call in viewLayoutSubView
UIView *myHeaderView = self.tableView.tableHeaderView;
[myHeaderView setNeedsLayout];
[myHeaderView layoutIfNeeded];
CGFloat height = [myHeaderView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
myHeaderView.frame = ({
CGRect headerFrame = myHeaderView.frame;
headerFrame.size.height = height;
headerFrame;
});
self.tableView.tableHeaderView = myHeaderView;
}
call it at viewDidLayoutSubView. Hope it helps bạn :))

How can I Use Animation On Navigation Right bar button?

I am using below code for Navigation right bar button:--
UIButton *settingsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[settingsBtn setImage:[UIImage imageNamed:#"logo_soft.png"] forState:UIControlStateNormal];
//[settingsBtn addTarget:self action:#selector(settings) forControlEvents:UIControlEventTouchUpInside];
[settingsBtn setFrame:CGRectMake(-16, -6, 95, 43)];
UIView *rightBarButtonItems = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 76, 32)];
//[rightBarButtonItems addSubview:homeBtn];
[rightBarButtonItems addSubview:settingsBtn];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButtonItems];
Now I want to use below code on same Navigation right bar button:
- (void)viewDidLoad
{
[super viewDidLoad];
// Animation
UIImageView*animationView = [[UIImageView alloc] initWithFrame:self.view.frame];
animationView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.gif"],
[UIImage imageNamed:#"2.gif"],
[UIImage imageNamed:#"3.gif"],
[UIImage imageNamed:#"4.gif"],
[UIImage imageNamed:#"5.gif"],
[UIImage imageNamed:#"6.gif"],nil];
animationView.animationDuration = 1.25;
animationView.animationRepeatCount = 0;
[animationView startAnimating];
[self.view addSubview:animationView];
}
How to work my navigation bar button like the below Images:---
Please send me suitable answer or provide me sample code.
Thank you.
I have tried your code, with a little trick of mine and it work like magic.
NSArray *imageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"tmp-0"],
[UIImage imageNamed:#"tmp-1"],
[UIImage imageNamed:#"tmp-2"],
[UIImage imageNamed:#"tmp-3"],
[UIImage imageNamed:#"tmp-4"],
[UIImage imageNamed:#"tmp-5"],
[UIImage imageNamed:#"tmp-6"],
[UIImage imageNamed:#"tmp-7"],nil];
UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barButton setImage:[UIImage imageNamed:#"tmp-0"] forState:UIControlStateNormal]; // mine trick
[barButton.imageView setAnimationImages:imageArray];
[barButton.imageView setAnimationDuration:1.0f];
[barButton.imageView startAnimating];
[barButton sizeToFit];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:barButton];
You need to give a image to barButton at first, so your button can make its frame

Image not stored in an NSMutableArray

i am trying to store image in NsMutable Array.But it not working in that function. But eventually in other function it work properly.can someone tell me why it is not working.
- (void)readPlistData {
objectButtonArray = [[NSMutableArray alloc] init]; \\Use to store Buttons
editButtonArray = [[NSMutableArray alloc] init]; \\ another array use to store deletebutton(such as.crossmark button.)
selectImage = [[NSMutableArray alloc] init];
self.myPlistPath = [NSString stringWithString:[self plistPath]];
plistArray = [[NSMutableArray alloc] initWithContentsOfFile:self.myPlistPath];
for (int i =0; i< [plistArray count];i++)
{
UIGraphicsEndImageContext();
NSData *imageData = [plistArray objectAtIndex:i];
currentObjectImage = [UIImage imageWithData:imageData] ;
[selectImage addObject:currentObjectImage]; \\value not stored in selectimage NSMutableArray
CGRect imageSize = CGRectMake(0, 0, 100, 90);
UIGraphicsBeginImageContext(imageSize.size); // this will crop
[currentObjectImage drawInRect:imageSize];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
imageButton = [[UIButton alloc]initWithFrame:CGRectMake(width, 0, 100, 90)];
[imageButton setTag:tag];
[imageButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[imageButton setImage:newImage forState:UIControlStateNormal];
CGRect backFrame = backView.frame;
backFrame.size.width = 120+width;
backView.frame = backFrame;
[backView addSubview:imageButton];
editButton = [[UIButton alloc]initWithFrame:CGRectMake(width-10, -10, 35, 35)];
[editButton setImage:[UIImage imageNamed:#"DeleteButton.png"] forState:UIControlStateNormal];
[editButton addTarget:self action:#selector(deleteObjectViewImage:) forControlEvents:UIControlEventTouchUpInside];
editButton.hidden = YES;
[editButton setTag:tag];
[backView addSubview:editButton];
tag++;
width = 120 + width;
[objectButtonArray addObject:imageButton];
[editButtonArray addObject:editButton];
}
scrollView.contentSize = backView.bounds.size;
}
can someone help me.
I think the problem is Reference name "currentObjectImage" basically when you are doing
[selectImage addObject:currentObjectImage];
it saves the reference of "currentObjectImage", where in the next iteration next image is loaded in it which creates problem.
Replace above line with
[selectImage addObject:[currentObjectImage copy]];
I think it will solve your problem.