Adding a UILabel to a UIImageView - uiimageview

In my app, I am saving an image view with drawings on it. When I try to add the label as a subview to the image view, the image is successfully saved but does not have the label on it. Any thoughts? mainImage is the UIImageView and playNameDisplay is the UILabel.
Here is my code:
UIGraphicsBeginImageContextWithOptions(self.mainImage.bounds.size, NO, 0.0);
[self.mainImage.image drawInRect:CGRectMake(0, 0, self.mainImage.frame.size.width, self.mainImage.frame.size.height)];
[mainImage addSubview:playNameDisplay];
UIImage *SaveImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(SaveImage, self,#selector(image:didFinishSavingWithError:contextInfo:), nil);

If you're using iOS7 you can use the new snapshotting api from apple.
UIGraphicsBeginImageContextWithOptions(image.size, NULL, 0);
[view drawViewHierarchyInRect:rect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
The view you call drawViewHierarchyInRect should be your UIImageView that has your UILabelView as a subview.If you have access to the Apple WWDC 2013 videos you should check out Implementing Engaging UI on iOS. It has some good code examples and also reviews the performance improvement over the more traditional methods.

Just try with bring subview to front property and check.Like this:
if Label is added as a sub view of image view use this.
[self.mainImage bringSubViewToFront:playNameDisplay];
if Label and image view is added as a sub view in view:(Nothing but both image view and label over laps) use this.
[self.view bringSubviewToFront:playNameDisplay];

Related

Change image when pressing uiimageview

i am doing a very simpel memory game to a ipad.
I want to change the image on the uiimageview when pressing it, and change it back when pressing it again.
Thanks for any help!
Regards
Using Tap gesture recognizer to change the imageview's image.I upload my code for tap gesture on image.
Note : Don't forget to imageview's UserInteraction is enable.
The below code write in your ViewDidLoad method.
UITapGestureRecognizer *firstdoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(firstTapGesture:)];
firstdoubleTap.numberOfTapsRequired = 1;
[_firstimageview setUserInteractionEnabled:YES];
[_firstimageview addGestureRecognizer:firstdoubleTap];
Than Your action method.
- (void)firstTapGesture:(UITapGestureRecognizer*)sender
{
_firstimageview.image=[UIImage imageNamed:#"abcd.png"];
}

Change height of formSheet in iOS 8

I just have one question related to presenting a from sheet view controller in iOS 8. In iOS 7 I was able to change the height of the view controller using the last line of code in the function below:
SendRemainingEvaluationsViewController *sendRemainingEvaluationViewController = [[[SendRemainingEvaluationsViewController alloc] initWithNibName:#"SendRemainingEvaluationsViewController" bundle:nil]autorelease];
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:sendRemainingEvaluationViewController] autorelease];
navigationController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navigationController animated:YES completion:nil];
//To Change the default size of the viewController which is presented in UIModalPresentationFormSheet presentation style
navigationController.view.superview.frame = CGRectMake(navigationController.view.superview.frame.origin.x, navigationController.view.superview.frame.origin.y, navigationController.view.superview.frame.size.width, 230);
But this does not work on iOS 8, not sure why? Does anyone knows what is the problem? any idea will be appreciated.
Thanks in advance.
After checking it, I could figure out the problem. After initializing the view navigation controller, its superview is nil and you can't access it right after presenting it. As a workaround, you can change the frame of its superview in the viewwillAppear of the view controller you're trying to present as you can see below:
- (void) viewWillAppear:(BOOL)animated
{
self.parentViewController.view.superview.frame = CGRectMake(self.parentViewController.view.superview.frame.origin.x, self.parentViewController.view.superview.frame.origin.y, self.parentViewController.view.superview.frame.size.width, 230);
}
This should work.

how can i have imageView.animationImages combined with UISwipeGestureRecognizer

I have a viewController displaying images and text and the images are loaded and being animated every 15 seconds, I'd like to allow the user to swipe on the image view allowing him to change the image without waiting for the time to pass.
this is what I tried the images are loaded and animated but the swipe gesture doesn't work
self.imageView.animationImages = [self loadImages];
self.imageView.animationDuration = 15;
[self.imageView startAnimating];
swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeAction:)];
[swipeRight setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.imageView addGestureRecognizer:swipeRight];
this code is placed in the viewDidLoad method, I saw a solution adding the gesture to the view but I need it only on the imageView
I'm answering this in case someone else spends too much time on this one it appears i forgot to enable user interaction add this line:
self.imageView.userInteractionEnabled = YES;

Xcode 4.2.1 UIScrollViews not scrolling in storyboard

So basically I have this project, where I have 4 different tab bar pages. 2 of them uses navigation controller & tab bar, and 2 are just view controllers. Now there is this one viewcontroller, which I need to add a scroll view to. So basically, I click on the item in the tab bar, it takes me to a view controller, where I can scroll down and up. I have been following Youtube Video Link, but I do not get it to work. The problem is that I use the exact code, and I change the ViewController class to the .h and .m name (ScrollViewViewController), and I put in a scroll view that has 320 x 1000, and a button at the top, but it doesn't scroll! How can I solve this problem?
Note: That if you don't know my problem, but could very detailed walk me through on how to add a scroll view to storyboards in xcode 4.2, then that would be amazing :)!
scrollerViewController.m:
- (void)viewDidLoad {
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 1000)];
[super viewDidLoad];
}
scrollerViewController.h
#interface scrollerViewController : UIViewController {
IBOutlet UIScrollView *scroller;
}
#end
And here comes a picture of the storyboard.
I need some of the code in order to really know your problem, but let me throw in my guess. You may be setting the size 320x1000 to the frame size of the scrollview by any chance? The frame size of the scrollview has to stay 320x480, and the contentSize should be 320x1000. Sorry if it was a wrong guess, but shouldn't do any harm :)
Something like this in your ViewController of the scrollView...
- (void) viewDidLoad {
[super viewDidLoad];
UIScrollView * scrollView = self.view;
scrollView.frame = (CGRect){scrollView.frame.origin, CGSizeMake(320, 480)};
scrollView.contentSize = CGSizeMake(320, 1000);
scrollView.backgroundColor = [UIColor whiteColor]; // I am setting the white background, so that the scroll indicator is visible
}
I am assuming you are setting ScrollView as the top view of the ViewController in your Storyboard as in the attached image.
I followed the exact same movie and it did not work for me either. But this movie did: http://www.youtube.com/watch?v=YBlwepYK7Zs
I followed that one and now i got it to work.
A bit late but might be more people looking for a solution to this.

Adding CPTGraphHostingView as a subview

There isn't a lot of questions about this and none of them were answered in a way that could solve my problem, still sorry if I'm asking something that was asked already.
My problem is that I'd like to use core plot to draw a graph inside a view. So I add the view on IB in my appviewcontroller.xib , but thenI can't find how to link that specific zone I just defined on IB to the file's owner.
A bit of precision: I managed to use core plot in another application, when it was the main view, but now that I want to add it to an existing application, so an existing view, it doesn't work...
And to add the new zone, I just went into IB, and dragged a view from the objects library onto my appviewcontroller.xib.
So how do I link that particular view to the code I wrote for it ?
Thanks for any help !
U have to add uiview created to self.view,then add graph hosting view to it...
Try this
- (void)loadView {
self.view =view1;
}
- (void)viewDidLoad {
[super viewDidLoad];
CPGraphHostingView *hostingView = [[CPGraphHostingView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
hostingView.backgroundColor=[UIColor whiteColor];
graph = [[CPXYGraph alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
hostingView.hostedGraph = graph;
[view1 addSubview:hostingView];
Create another view(view 2) in a main view(default view let it be view)in the storyboard(xib file) and make sure it (view 2) as outlet in the header file(.h).
Drag and Connect it to the newly created view in the storyboard(xib file) so after making necessary connections in the storyboard,storyboard should be like this:
View---view (default view connected to view) and
view 2---view (new view, view 2 connected to its view).
In the loadView method of implementation file,try out these code
-(void)loadView {
[super loadView];
newView = [[CPTGraphHostingView alloc] initWithFrame: [[UIScreen mainScreen]applicationFrame]];
[self.view2 addSubview:newView];
}

Resources