I'm having some issues when trying to capture more than 1 photo. The app becomes slow and then it crashes because of several memory warning. I am using Brad's GPUImage library. Here are some methods in which I use it:
//the creation of the camera view
- (IBAction)photoFromCamera
{
imageView = [[GPUImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:imageView];
stillCamera = [[GPUImageStillCamera alloc] init];
stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
//stillCamera.outputImageOrientation = UIInterfaceOrientationLandscapeLeft;
filter = [[GPUImageFilter alloc] init];
[filter prepareForImageCapture];
[stillCamera addTarget:filter];
[stillCamera addTarget:imageView];
[filter addTarget:imageView];
[stillCamera startCameraCapture];
UIButton * shotButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
shotButton.frame = CGRectMake(137, 361, 46, 30);
[shotButton setTitle:#"Take!" forState:UIControlStateNormal];
[shotButton addTarget:self action:#selector(shotAction:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:shotButton];
UIButton * cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cancelButton.frame = CGRectMake(10, 80, 55, 40);
[cancelButton setTitle:#"cancel" forState:UIControlStateNormal];
[cancelButton addTarget:self action:#selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:cancelButton];
}
//The take photo action
-(void)shotAction:(UIImagePickerController *)photoPicker{
[stillCamera capturePhotoAsPNGProcessedUpToFilter:filter withCompletionHandler:^(NSData *processedPNG, NSError *error) {
self.saveButton.enabled = YES;
self.filterButton.enabled = YES;
self.cropButton.enabled = YES;
originalFirstImage = [UIImage imageWithData:processedPNG];
[self.selectedImageView setImage:originalFirstImage];
}];
[imageView removeFromSuperview];
}
is there something wrong? I'm using ARC, is there something that is not releasing?
Thanks for your comments.
EDIT: the error is: Terminated due to Memory Pressure
Shot action method have block, and block using self. Block always capture its environment, means it will reference every thing strongly. So it will cause a retain cycle, which will not allow to free memory. You should access self with weak pointer. I will suggest you that
__weak typeof(self) weakSelf = self;
and use this weakSelf in block.
EDIT
GPUImage library also have some memory leak issues reported, read this forum
Related
In the arc naming convention says : the init*** method 's return value will be retained by caller, but if i wrote a init*** method without return value , Is it will cause some memory problem in ARC?
- (void)initToolbar
{
// 工具栏
UIButton *commendEditButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 0, [Utils getScreenAppSize].width - 15 *2, 32)];
commendEditButton.backgroundColor = [UIColor whiteColor];
commendEditButton.layer.borderColor = UIColorFromRGB(0xb6b6b6).CGColor;
commendEditButton.layer.cornerRadius = 4.f;
commendEditButton.layer.borderWidth = .5f;
[commendEditButton addTarget:self action:#selector(showCommentCompose) forControlEvents:UIControlEventTouchUpInside];
[commendEditButton setImage:[UIImage imageNamed:#"comment-ico-compose"] forState:UIControlStateNormal];
[commendEditButton setImage:[UIImage imageNamed:#"comment-ico-compose"] forState:UIControlStateHighlighted];
[commendEditButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[commendEditButton setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
[commendEditButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 15, 0, 0)];
[commendEditButton.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
[commendEditButton setTitleColor:UIColorFromRGB(0xc4c4c4) forState:UIControlStateNormal];
[commendEditButton setTitle:#"发表评论" forState:UIControlStateNormal];
NSArray *toolbarItems = [NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithCustomView:commendEditButton],nil];
_toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, [Utils getScreenSize].height - UI_TOOL_BAR_HEIGHT - UI_STATUS_BAR_HEIGHT - UI_NAVIGATION_BAR_HEIGHT, [Utils getScreenAppSize].width, UI_TOOL_BAR_HEIGHT)];
[_toolBar setBackgroundImage:[UIImage imageNamed: #"toolbar_bg.png"] forToolbarPosition:UIToolbarPositionBottom barMetrics:UIBarMetricsDefault];
_toolBar.items = toolbarItems;
}
Nothing really bad will happen if you don't count violating many code quality rules.
#implementation A
- (void)initTest {
// do nothing
}
#end
A *instance = [A alloc];
[instance initTest];
Note the instance is still strongly referenced, we didn't even call [super init].
But we are very lucky here because init on NSObject does nothing.
In practice, this is very dependent on the superclass and in theory this is undefined behavior. The current implementation allows everything to work correctly but it's terrible code indeed.
I have a view controller which is being added as a child view controller. The problem is that after the child controller creates its view, I am not able to modify it.
It loads everything properly, but when I want to hide a text field on clicking a button (textfield.hidden = YES), nothing happens!
This is my loadView :
-(void)loadView {
self.waitingPopup = [[WaitingPopupViewController alloc]initWithLabel:#"loading salon"];
self.view=[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
// self.view.frame=CGRectMake(0, -20, self.view.frame.size.width, self.view.frame.size.height);
scrollableTable=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scrollableTable.delegate=self;
scrollableTable.dataSource=self;
[self.view addSubview:scrollableTable];
headerHolder=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-44)];
//creating and adding backgroundImage view container and setting the image
// saloonHomeImage.backgroundColor = [UIColor grayColor];
saloonHomeImage.frame=CGRectMake(self.view.frame.origin.x, 0, self.view.frame.size.width, self.view.frame.size.height);
saloonHomeImage.contentMode = UIViewContentModeScaleAspectFill;
//[saloonHomeImage setBackgroundColor:[UIColor redColor]];
[headerHolder setBackgroundColor:[UIColor grayColor]];
[headerHolder addSubview:saloonHomeImage];
//creating and adding saloonheader label
saloonLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 32, self.view.frame.size.width, 48)];
saloonLabel.text=#"";
saloonLabel.textAlignment=NSTextAlignmentCenter;
[saloonLabel setTextColor:[UIColor whiteColor]];
[saloonLabel setFont:[UIFont fontWithName:#"vevey" size:48]];
[headerHolder addSubview:saloonLabel];
//creating and adding book appointment button
UIButton *bookAppointmentButton=[UIButton buttonWithType:UIButtonTypeSystem];
NSLog(#"%f", self.view.frame.size.height-97.5);
bookAppointmentButton.frame=CGRectMake(self.view.frame.origin.x, self.view.frame.size.height-97.5, 216, 53);
[bookAppointmentButton setBackgroundImage:[UIImage imageNamed:#"bg-left.png"] forState:UIControlStateNormal];
[bookAppointmentButton setTitle:#"BOOK APPOINTMENT" forState:UIControlStateNormal];
[bookAppointmentButton addTarget:self action:#selector(bookAppointmentAction) forControlEvents:UIControlEventTouchUpInside];
[bookAppointmentButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[headerHolder addSubview:bookAppointmentButton];
UIButton *callButton=[UIButton buttonWithType:UIButtonTypeSystem];
callButton.frame=CGRectMake(216, self.view.frame.size.height-97.5, self.view.frame.size.width-216, 53);
[callButton setBackgroundImage:[UIImage imageNamed:#"bg-right.png"] forState:UIControlStateNormal];
[callButton addTarget:self action:#selector(callButtonAction) forControlEvents:UIControlEventTouchUpInside];
[callButton setTitle:#"CALL" forState:UIControlStateNormal];
[callButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[headerHolder addSubview:callButton];
[self getSalonDetails];
loading=[[UIImageView alloc] init];
loading.frame=CGRectMake(130,200,40,36);
[loading setBackgroundColor:[UIColor clearColor]];
loading.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"spinner-1.png"],
[UIImage imageNamed:#"spinner-2.png"],
[UIImage imageNamed:#"spinner-3.png"],
[UIImage imageNamed:#"spinner-4.png"],
[UIImage imageNamed:#"spinner-5.png"],
[UIImage imageNamed:#"spinner-6.png"],
[UIImage imageNamed:#"spinner-7.png"],
[UIImage imageNamed:#"spinner-8.png"],[UIImage imageNamed:#"spinner-9.png"],nil];
loading.animationDuration = 1.0f;
loading.animationRepeatCount = 0;
[loading startAnimating];
[self.view addSubview:loading];
loadingText = [[UILabel alloc]initWithFrame:CGRectMake(90, 240, 200, 40)];
loadingText.text = #"loading salon";
[loadingText setFont:GOTHIC_FONT(22)];
[loadingText setTextColor:[UIColor whiteColor]];
[self.view addSubview:loadingText];
articles=[Model getArticles];
}
- (void) getSalonDetails{
[[MyDataModel sharedDataModel] getSalonDetails];
}
This is the delegate method which is trying to set some fields hidden. This delegate is called when an NSURLSession call completes successfully. The control comes here as per logs but nothing gets hidden. When I add a breakpoint and inspect the fields, every object is shown as nil. If this class is used as the main view controller, I've found everything works. On making it a child view, nothing works.
-(void)salonDetailsRecievedDelegate:(BOOL)success andStatusCode:(int)statusCode{
[loading stopAnimating];
self.loading.hidden = YES;
self.loadingText.hidden = YES;
saloonLabel.text = #"this is texT";
if(success){
//
}
}
What I think here is that salonDetailsRecievedDelegate isn't called from the main thread, try any of the following:
1) call it in the main thread from your model as follows:
dispatch_async(dispatch_get_main_queue(), ^{
[self.delegate salonDetailsRecievedDelegate:success andStatusCode:status];
});
2) change its implementation as follows:
-(void)salonDetailsRecievedDelegate:(BOOL)success andStatusCode:(int)statusCode{
dispatch_async(dispatch_get_main_queue(), ^{
[loading stopAnimating];
self.loading.hidden = YES;
self.loadingText.hidden = YES;
saloonLabel.text = #"this is texT";
if(success){
//
}
});
}
In my project i used the below code for a purpose and it is working properly.
My code:
UIView *view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.jpg",index]]] autorelease];
view.frame = CGRectMake(70, 80, 180, 260);
return view;
But here instead of UIImageView i would like to use UIButton,So I replaced the first lin eof above code by:
UIView *view=[[UIButton alloc] setBackgroundImage:(UIImage *)[scrollImages objectAtIndex:index] forState:UIControlStateNormal];
But at this point i am getting an error message as
"Initializing 'UIView*_strong' with an expression of incompatible type 'void'".
Can anyone help me to solve this by replacing UIImageView with UIButton....
You should init the instance of UIButton after created it via `alloc'
UIButton *button = [[UIButton alloc] init];
[button setBackgroundImage:(UIImage *)[scrollImages objectAtIndex:index] forState:UIControlStateNormal];
UIView *view = button;
You must use an init... method to complete the initialization process
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state return nothing (void), it's certainly incompatible type with UIView.
Like this...
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(pressed:) forControlEvents:UIControlEventTouchUpInside];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"%d.jpg",index]];
[button setImage:image forState:UIControlStateNormal];
button.frame = CGRectMake(70, 80, 180, 260);
[self.view addSubview:button];
i am creating one iPad application in that i want to open media player to play video. For this i create one button in ViewDidLoad() method. Bellow is my code.
- (void)viewDidLoad
{
[super viewDidLoad]
self.view.backgroundColor = [[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"1.png"]] autorelease];
m_btn2000 = [[UIButton buttonWithType:UIButtonTypeCustom] autorelease];
m_btn2000.frame = CGRectMake(180, 330, 130, 200);
[m_btn2000 setImage:[UIImage imageNamed:#"2.png"] forState:UIControlStateNormal];
m_btn2000.clipsToBounds = YES;
[m_btn2000 addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:m_btn2000];
}
Bellow is my button clicked method code:
- (void)btnClicked:(id)sender
{
NSString *filePath1 = [[NSBundle mainBundle] pathForResource:#"test_video" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath1];
m_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
m_moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[m_moviePlayer.view setFrame:CGRectMake(0, 0, 1026, 748)];
[self.view addSubview:m_moviePlayer.view];
[m_moviePlayer play];
}
when i m going to click on button it gives me EXC_BAD_ACCESS error.
But i notice one thing that when i put above whole code which in button clicked method putting in ViewDidLoad() method it play a video in player proper way.
The reason is : you are creating a button with + (factory method) and you are sending autorelease message.
you need to change this line from:
m_btn2000 = [[UIButton buttonWithType:UIButtonTypeCustom] autorelease];
to
m_btn2000 = [UIButton buttonWithType:UIButtonTypeCustom];
After looking through the post on this simple topic I still can not figure out what I've done wrong here. I am trying to make the buttons loop through the same action in the same viewController.
When the view comes onto the screen the buttons do not click (or at least the NSLog does not write to the console which it does the first time it enters this code.)
- (IBAction)answer: (id) sender{
NSLog(#"The Number is: %d\n",qNumber);
qNumber+=1;
UIView *newView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[newView setUserInteractionEnabled:TRUE];
UIImage *buttonImage = [UIImage imageNamed:#"pink_button.png"];
UIImageView *buttonImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(23, 294, 72, 37)] autorelease];
[buttonImageView setImage:buttonImage];
UILabel* buttonLabel = [[[UILabel alloc] initWithFrame:CGRectMake(23, 294, 72, 37)] autorelease];
buttonLabel.text = #"newLow";
buttonLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size: 15.0];
buttonLabel.textColor = [UIColor blackColor];
buttonLabel.backgroundColor = [UIColor clearColor];
buttonLabel.textAlignment = UITextAlignmentCenter;
lowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[lowButton addSubview:buttonImageView];
[lowButton addSubview:buttonLabel];
[lowButton addTarget:self action:#selector(answer:) forControlEvents:UIControlEventTouchUpInside];
[newView addSubview:lowButton];
self.view = newView;
}
Thanks for any help you can provide...even if I missed something simple :-)
Latest Updated Code:
lowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[newView addSubview:buttonImageView];
[newView addSubview:buttonLabel];
[lowButton addTarget:self action:#selector(answer:) forControlEvents:UIControlEventTouchUpInside];
[newView addSubview:lowButton];
[self.view bringSubviewToFront:lowButton];
self.view = newView;
Still when the button is pressed it does not enter the action code.
You button needs a frame. You can't tap a frameless button.
lowButton.frame = CGRectMake(23, 294, 72, 37);
Also, when you add label and image to that button, their frame should have x and y 0.
UIImageView *buttonImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 72, 37)] autorelease];
...
UILabel* buttonLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 72, 37)] autorelease];
...
But may I ask why you are adding new UIImageView and UILabel to UIButton when it has it's label and background view properties?
Edit here is a cleaned-up code for you:
UIView *newView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
lowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[lowButton setImage:[UIImage imageNamed:#"pink_button.png"] forState:UIControlStateNormal];
[lowButton setTitle:#"newLow" forState:UIControlStateNormal];
[lowButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[lowButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0]];
[lowButton addTarget:self action:#selector(answer:) forControlEvents:UIControlEventTouchUpInside];
[newView addSubview:lowButton];
self.view = newView;
Notice I also removed [newView setUserInteractionEnabled:TRUE]; as it's not needed since it's enabled by default.
Your custom button's subviews are covering the button, so it does not get any clicks.
Try adding all the subviews to newView rather than to lowButton (including lowButton itself) and then call
[self.view bringSubViewToFront:lowButton];