Objective-C: iPad Crash When Screen Shot Button Clicked - objective-c

OK, i am making a Coloring app and it has a "Save" button that will take the screen shot of the view where the drawing is.
I have 6 images to color. In the intro page there 6 buttons, each will link you to a particular coloring page.
All are working well including the "Save" button except for one. All coloring views has the same contents and functions but in one image, it crashes the iPad whenever i clicked the "Save" button.
Here's some of my codes:
On Button Click:
[self saveScreenshotToPhotosAlbum];
camFlash = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[camFlash setBackgroundColor:[UIColor whiteColor]];
[camFlash setAlpha:0.0];
[cbHolder addSubview:camFlash];
[cbHolder bringSubviewToFront:camFlash];
[UIView beginAnimations:#"Flash-On" context:NULL];
[UIView setAnimationDuration:0.2];
[UIView setAnimationRepeatAutoreverses:NO];
[camFlash setAlpha:1.0];
[UIView commitAnimations];
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"flash" ofType:#"wav"];
AVAudioPlayer *flashSFX = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
flashSFX.volume = 0.5;
flashSFX.numberOfLoops = 0;
self->camFlashSFX = flashSFX;
if ([flashSFX currentTime] != 0) [flashSFX setCurrentTime:0];
[flashSFX play];
[self performSelector:#selector(removeFlash) withObject:nil afterDelay:0.2];
Remove Flash:
- (void) removeFlash
{
[camFlash removeFromSuperview];
}
For the screenshot:
- (UIImage*)captureView {
UIGraphicsBeginImageContext(CGSizeMake(1024, 768));
CGContextRef context = UIGraphicsGetCurrentContext();
[cbHolder.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
- (void)saveScreenshotToPhotosAlbum {
UIImageWriteToSavedPhotosAlbum([self captureView], nil, nil, nil);
}
This one is for the button that will show the coloring page:
uc4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tpUncolored1.png"]];
[uc4 setUserInteractionEnabled:YES];
[uc4 setFrame:CGRectMake(0, 0, 1024, 768)];
[cbHolder addSubview:uc4];
clrd4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tpColored1.png"]];
[clrd4 setUserInteractionEnabled:YES];
[clrd4 setFrame:CGRectMake(0, 0, 1024, 768)];
[cbHolder addSubview:clrd4];
[UIView beginAnimations:#"fabe-out" context:NULL];
[UIView setAnimationDuration:1.5];
[clrd4 setAlpha:0.0];
[UIView commitAnimations];
[self performSelector:#selector(cb4) withObject:nil afterDelay:1.5];
AND I AM HAVING THIS ERROR:
Thread 5: EXC_BAD_ACCESS (code=1, address=0xd9bbd40)

Related

Recreating the iMessage app [duplicate]

This question already has answers here:
Programmatically align a toolbar on top of the iPhone keyboard
(7 answers)
Closed 9 years ago.
I am doing a messaging app and I am trying to duplicate the toolbar on top of the key board with the UITextField and two buttons (camera and done). I know how to put a toolbar on top of the keyboard when you click on a UITextField in the view, but my textfield is on my toolbar. When the UITextField on the toolbar is clicked, I want the keyboard to come up under it and kind of push the toolbar up with it. I also need them to go down together as if they are one thing (like it does in the iMessage app). I have looked up many tutorials but cannot seem to find the answer. What code do I need to do this?? Thank you I AM NOT ASKING HOW TO PUT A TOOLBAR ON TOP OF A KEYBOARD, I AM ASKING HOW TO TRIGGER THE KEYBOARD TO COME UP UNDER THE EXISTING TOOLBAR AND BE TRIGGERED BY THE TEXTVIEW INSIDE SAID TOOLBAR
i have written this code duplicate the effect of iMessage, it uses a open source textview HPGrowingTextView
- (void)organizeView {
textView = [[HPGrowingTextView alloc] initWithFrame:CGRectMake(8, 5, 230, 40)];
textView.contentInset = UIEdgeInsetsMake(0, 5, 0, 5);
textView.minNumberOfLines = 1;
textView.maxNumberOfLines = 4;
// you can also set the maximum height in points with maxHeight
// textView.maxHeight = 200.0f;
textView.returnKeyType = UIReturnKeySend; //just as an example
textView.delegate=self;
textView.font = [UIFont systemFontOfSize:15.0f];
textView.delegate = self;
textView.internalTextView.scrollIndicatorInsets = UIEdgeInsetsMake(5, 0, 5, 0);
textView.backgroundColor = [UIColor whiteColor];
textView.placeholder = #"Type your Comment here..";
[textView becomeFirstResponder];
UIImage *rawEntryBackground = [UIImage imageNamed:#""];
UIImage *entryBackground = [rawEntryBackground stretchableImageWithLeftCapWidth:13 topCapHeight:22];
UIImageView *entryImageView = [[UIImageView alloc] initWithImage:entryBackground];
entryImageView.frame = CGRectMake(5, 0, 248, 40);
entryImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
UIImage *rawBackground = [UIImage imageNamed:#"message-strip.png"];
UIImage *background = [rawBackground stretchableImageWithLeftCapWidth:13 topCapHeight:22];
UIImageView *imageView = [[UIImageView alloc] initWithImage:background];
imageView.frame = CGRectMake(0, 0, _containerView.frame.size.width, _containerView.frame.size.height);
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// view hierachy
[_containerView addSubview:imageView];
[_containerView addSubview:textView];
[_containerView addSubview:entryImageView];
UIImage *sendBtnBackground = [[UIImage imageNamed:#"send"] stretchableImageWithLeftCapWidth:13 topCapHeight:0];
UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
doneBtn.frame = CGRectMake(_containerView.frame.size.width - 69, 8, 63, 27);
doneBtn.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
[doneBtn setImage:sendBtnBackground forState:UIControlStateNormal];
[doneBtn addTarget:self action:#selector(postButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[_containerView addSubview:doneBtn];
_containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
}
-(void)resignTextView
{
[textView resignFirstResponder];
}
//Code from Brett Schumann
-(void) keyboardWillShow:(NSNotification *)note{
// get keyboard size and loctaion
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
// Need to translate the bounds to account for rotation.
keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
NSInteger kbSizeH = keyboardBounds.size.height;
// get a rect for the textView frame
CGRect containerFrame = _containerView.frame;
containerFrame.origin.y -= kbSizeH-50;
////reset the table container view height
CGRect tableContainerFrame=_viewTable.frame;
tableContainerFrame.size.height=self.view.frame.size.height-(kbSizeH+containerFrame.size.height );
//containerFrame.origin.y = self.view.bounds.size.height - (keyboardBounds.size.height + containerFrame.size.height);
// animations settings
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
// set views with new info
_containerView.frame = containerFrame;
_viewTable.frame=tableContainerFrame;
// commit animations
[UIView commitAnimations];
}
-(void) keyboardWillHide:(NSNotification *)note{
NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
// get a rect for the textView frame
CGRect containerFrame = _containerView.frame;
containerFrame.origin.y = self.view.bounds.size.height - containerFrame.size.height;
// animations settings
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
// set views with new info
_containerView.frame = containerFrame;
// commit animations
[UIView commitAnimations];
}
- (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height
{
float diff = (growingTextView.frame.size.height - height);
CGRect r = _containerView.frame;
r.size.height -= diff;
r.origin.y += diff;
_containerView.frame = r;
}
-(BOOL)growingTextViewShouldReturn:(HPGrowingTextView *)growingTextView{
if (growingTextView == self->textView) {
[self performSelector:#selector(postButtonAction:)];
return NO;
}
else
return YES;
}

I need to popup actionsheet above tabbar in my present viewcontroller, but it should not overlap tabbar(i need to see tabbar clearly)

I need to popup transperant actionsheet above tabbar in my present viewcontroller, but it should not overlap tabbar(i need to see tabbar clearly).
To create a transparant background on a UIActionsheet you have to set the delegate and then you can do:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
[actionSheet.layer setContents:nil];
}
To create a custom UIView that animates in from the bottom you can do>
bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 250)];
bottomView.backgroundColor = [UIColor redColor];
bottomView.hidden = YES;
[self.view addSubview:bottomView];
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeCustom];
testButton.frame = CGRectMake(20, 50, 280, 40);
testButton.backgroundColor = [UIColor greenColor];
[bottomView addSubview:testButton];
[self animateBottomViewIn];
And have the following methods:
-(void)animateBottomViewIn
{
bottomView.hidden = NO;
[UIView animateWithDuration:0.3 animations:^{
bottomView.frame = CGRectMake(0, self.view.frame.size.height - bottomView.frame.size.height, self.view.frame.size.width, bottomView.frame.size.height);
} completion:^(BOOL finished) { }];
}
-(void)animateBottomViewOut
{
[UIView animateWithDuration:0.3 animations:^{
bottomView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, bottomView.frame.size.height);
} completion:^(BOOL finished) {
bottomView.hidden = YES;
}];
}

iOS – Animation glitch with UIToolbar

I'm getting a "weird" animation glitch when I add a UIToolbar to the view. I'm animating the UIToolbar so it slides up from the bottom together with either a UIPickerView or a UIDatePicker. At first I see it's sliding up but after the animation is finished it quickly disappears. This happens when I slide down (animating the UIPickerView to slide down off the screen) and right after that I have another UIPickerView slide up.
I noticed that if I set a delay (- performSelector...) on the slide up call for 0.3 seconds it will display the UIToolbar properly (anything less than 0.3 seconds will still have the same glitch). What might be causing this?
EDIT: Perhaps I should place both the UIToolbar and UIDatePicker in a new UIView container?
This is the code I'm using:
if ([self.view.subviews containsObject:self.dateRepeatPicker]) {
[self dismissDateRepeatPickerSegmentChanged:NO];
// "Hack", if I don't delay this call the UIToolbar will not display
[self performSelector:#selector(showDatePicker) withObject:nil afterDelay:0.3];
} else if (![self.view.subviews containsObject:self.datePicker]) {
[self showDatePicker];
}
- (void)dismissDateRepeatPickerSegmentChanged:(BOOL)segmentChanged {
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height, 320, 44);
CGRect dateRepeatPickerTargetFrame = CGRectMake(0, self.view.bounds.size.height+44, 320, 216);
[UIView animateWithDuration:0.2
animations:^{
self.pickerToolbar.frame = toolbarTargetFrame;
self.dateRepeatPicker.frame = dateRepeatPickerTargetFrame;
}
completion:^(BOOL finished) {
if (segmentChanged) {
[self.pickerToolbar removeFromSuperview];
[self.dateRepeatPicker removeFromSuperview];
[self.remindMeTableView reloadData];
} else {
[self.pickerToolbar removeFromSuperview];
[self.dateRepeatPicker removeFromSuperview];
}
}];
}
- (void)showDatePicker {
// Create the Toolbar over the Picker
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44);
if (self.pickerToolbar == nil) {
self.pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
self.pickerToolbar.barStyle = UIBarStyleBlack;
self.pickerToolbar.translucent = YES;
}
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"picker-dismiss.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(dismissDatePickerToolbar)];
[self.pickerToolbar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
[self.view addSubview:self.pickerToolbar];
// Create the Picker under the Toolbar
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216);
if (self.datePicker == nil) {
self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)];
[self.datePicker setMinuteInterval:5];
[self.datePicker addTarget:self action:#selector(changeDate:) forControlEvents:UIControlEventValueChanged];
}
[self.view addSubview:self.datePicker];
[UIView animateWithDuration:0.2 animations:^{
self.pickerToolbar.frame = toolbarTargetFrame;
self.datePicker.frame = datePickerTargetFrame;
}];
}
Solved by adding the UIPickerView and the UIToolbar to a containing UIView and then animating the containing UIView.

Objective-C - UIControlEventTouchUpInside not fired?

I'm creating an animated UIButton to simulate an insterstitial banner, but it's not getting the event UIControlEventTouchUpInside. Here is my code:
-(void)insertInterstitialBannerAtView:(UIView *)mainView {
if ([self bannerExists]) {
bannerButton = [UIButton buttonWithType:UIButtonTypeCustom];
[bannerButton setFrame:CGRectMake(0, 480, 320, 43)];
[bannerButton setImage:[UIImage imageNamed:#"389.png"] forState:UIControlStateNormal];
[bannerButton setEnabled:YES];
[mainView addSubview:bannerButton];
[mainView bringSubviewToFront:bannerButton];
[bannerButton addTarget:self
action:#selector(buttonPushed)
forControlEvents:UIControlEventTouchUpInside];
[UIView animateWithDuration:2.0
animations:^(void) {
[bannerButton setFrame:CGRectMake(0, 417, 320, 43)];
}
completion:^(BOOL finished) {
[UIView animateWithDuration:2.0 delay:3.0 options:UIViewAnimationTransitionNone animations:^(void){
[bannerButton setFrame:CGRectMake(0, 480, 320, 43)];
}
completion:^(BOOL finished) {
}];
}];
}
}
-(IBAction)buttonPushed{
NSLog(#"Interstitial Pushed");
}
-(BOOL)bannerExists{
BOOL returnValue = FALSE;
NSArray *names = [URLFORSPLASH componentsSeparatedByString:#"/"];
NSString *fileName = [names lastObject];
NSString *cacheFolder = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [cacheFolder stringByAppendingPathComponent:fileName];
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath] ) {
returnValue = TRUE;
}
return returnValue;
}
Does anybody know what's wrong?
Regards,
Claudio
ios disables interactions during an animation. From the UIView documentation: http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW111
"During an animation, user interactions are temporarily disabled for the views being animated. (Prior to iOS 5, user interactions are disabled for the entire application.)"
If you're targeting ios 5, you might be able to get away with intercepting touches in the parent view and then checking to see if they're where the animation is/was when the touch was intercepted.

Animating the frame of a UISegmentedControl

I have a UISegmentedControl that I'm trying to animate the frame of using the following (simplified, but functional) code:
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"item1", #"item2", nil]];
[seg setFrame:CGRectMake(0, 300, 100, 50)];
[self addSubview:seg];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^ {
[UIView animateWithDuration:5.0 animations: ^ {
[seg setFrame:CGRectMake(100, 0, 400, 50)];
}];
});
What happens is the size changes immediately, but the position animates properly. Why is it doing this, and are there any workarounds?
Tell the segmented control to layout if needed in the animation block.
[UIView animateWithDuration:.25f animations:^{
self.segmentedControl.frame = frame;
[self.segmentedControl layoutIfNeeded];
}];
It seems that you can work around this by manually resizing all the subviews of the UISegmentedControl inside the animation block.