Translate CGPoint from UIView to AVCaptureVideoPreviewLayer - iphone

I'm trying to implement tap to focus and tap to exposure in my iPhone app.
I'm using the AVFoundation camera interface and I'm having trouble moving between the 2 co-ord systems.
UIView uses a system that goes from (0,0) in the top left (in portrait mode) to (320,480) in the bottom right.
However the AVCaptureSession uses a system that goes from (0,0) in the top right to (1,1) in the bottom left.
I've had a look at the AVCamDemo but it doesn't seem to do anything to convert these, one minute you have the UIView coords and the next you have the AV coords.
Any help is appreciated!
Thanks
Oliver

OK, I worked out what was going on. I'm writing the app at home though so can't update during the day :(
Anyway, the coord system for the touch is from (0,0) to (320,480) (top left to bottom right in portrait mode).
The coord system for the focal point of interest is (0,0) to (1,1) (top left to bottom right in landscape right mode).
In order to get the tap to focus you'll need an AVCaptureVideoPreviewLayer embedded into a subclass of UIView. (I'm assuming in this that the view is displayed across the entire screen).
To get from a tap to a focal point do the following.
Get the tap CGPoint (e.g. tapPoint).
Then use...
CGPoint focalPoint = CGPointMake(tapPoint.y/480, (320 - tapPoint.x)/320);
There may be a bit of code that does this for you but until then this will suffice.

I know this is an old question, but THERE WAS a function in AVCamDemo to do the conversion in all videoGravity cases (your code only work if videoGravity of the videoPreviewLayer is AVLayerVideoGravityResize)

Related

Cannot move the MKMapView from a centered position?

I am working with creating a simple iPhone app to show a map and some labels, however the MKMapView likes being centered when I put it in the interface builder.
I am very new to this, so i apologize for my ignorance.
I cannot seem to find a way to move the MKMapView from its centered position. I can scale the edges, however the scaling is mirrored on both sides and it does not let me alter the x and y value manually. I want to move the MKMapView to the top of the screen and have some labels on the bottom, however I don't want any extra space at the top.
Is there any method I can use (either within the Interface Builder OR in the actual Objective C code) which will let me move the MKMapView more freely?
Thank you,
-Serge
It sounds like you're attempting to place it in a bare window. If you want to position it on a portion of the screen somewhere you'll want to place a regular View in the window first (that could just fill the window), then put the MKMapView on that view. You'll be able to move it to wherever you'd like.

Inactive area after device rotation

I don't understand what's wrong in my very simple application with device rotation :
I built my view with interface builder. (See screen capture here)
I specified <key>UIInterfaceOrientation</key><string>UIInterfaceOrientationLandscapeRight</string> in my info.plist file.
I had a (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {return YES;} in my root view controller.
The area on the left (shown in red on the capture), around 20 pixel width, keeps inactive (nothing append if I hit a button in this area). In fact the full screen is active only in portrait mode, in landscape right mode there is this 20 pixels width inactive area, in landscape left mode this inactive area is on the right, in portrait upside down mode this area is on the bottom.
I read lots of posts and documentation about UIView rotation, but I did not find anything to solve this problem (I tried to play with view.frame and view.bounds without any success).
Anybody has an idea ?
Thanks a lot.
Regards.
Sébastien.
One of your views is probably not autoresized correctly.
After rotation, it still has its old bounds of, for example, 320 x 460. Since views normally don't clip their contained views you don't see a difference.
Events on the other hand are only delivered to views that are contained in their superviews bounds.

Cocos2D, UIScrollView, and initial placement of a scene

I am using a UIScrollView to forward touches to Cocos2D as outlined in http://getsetgames.com/2009/08/21/cocos2d-and-uiscrollview/
Everything works great after a few days of working with it, except one thing: when the initial view appears on the screen, the background appears to be scrolled to the center. As soon as I try to scroll around, the image jumps to 0,0, and everything works as normal, except the touches are offset by half the width and height of the background image. Am I overlooking something basic? I can't think of a useful portion of the code that illustrates the issue, as I can't track it down, but would be happy to post code if anyone has any ideas.
Thanks in advance,
-Roberto
I just had to localize the touches in respect to the window, by using nil in locationInView. This returns the touch's location on what's currently visible in the display.
CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:nil]];

Rotating UIWebView with transform, content partially off screen, how to get it to stick on screen?

I have a 320x416 portrait-shaped UIWebView filling a UIViewController's view. I also have a 90 degree rotate button that will transform the UIWebView through 90 degrees each time the button is touched. The code is basically:
webView.transform = CGAffineTransformMakeRotation(touches%4 * M_PI/2.0);
After rotation through 90 degrees, the now-landscape transformed UIWebView extends beyond both the left and right edges of the screen. In the process of applying the transformation, iPhone OS has changed the UIWebView's frame from {{0,0}, {320,416}} to {{-48,48}, {416,320}}. Don't have a problem with that.
I then tweak the UIWebView's frame origin to (0,0) so that it starts top-left, but extends a little further beyond the right edge of the screen. Now, I can touch the UIWebView and pull it left to view the hidden information on the right but I cannot get the right-hand end to to stay on the screen -- the moment I untouch it, the right side bounces back off the screen.
What is it that causes the view to bounce back off-screen? In other words, what is it that I need to tweak to allow either the left edge or the right edge to stick on the screen and remain visible (only one at a time, obviously)?
Thanks.
As far as I understand what you've done in your project, what you are seeing is the normal behaviour of any scrolling window in iPhoneOS. That is, your UIWebView is wide enough to contain all the HTML content displayed within so while it will move when you drag it, it springs back to its original position when you let go. The fact that the UIWebView is wider than the screen due to the fact that you've rotated it, but not resized it, means that you can't see it all the HTML content, but as far as the UIWebView is concerned it's all visible so it doesn't let you properly scroll it, it just bounces.

Flipping UIViews From Top / Bottom

I'm well aware that there are only two available UIView transitions, UIViewAnimationTransitionFlipFromLeft and UIViewAnimationTransitionFlipFromRight. I'm wondering if there is anyway that I can implement / emulate a UIViewAnimationTransitionFlipFromTop or UIViewAnimationTransitionFlipFromBottom.
The only way I can think to do this is by flipping the x axis with the y axis, but I've not seen any information about how to do this. Just setting the coordinates of each axis won't fix the issue as the x-axis till remains the x-axis.
Does anyone have any ideas how this can be accomplished?
You can do this by applying a CATransform3D to your main view's CALayer, with your secondary view's CALayer behind it. In this response, I point to a Mac implementation of this animation by Mike Lee. You should be able to reuse a significant portion of that code to recreate this effect on the iPhone.
Guess you'll have to use a UIView animation block with a 3D rotation transform of 90 degrees, have the Core Animation delegate call you when that's done, swap the view with the new one (3D rotated on the other side) and chain with the last 90 degrees for the new view...
Although its not exactly what you want, there are two built-in animations that you didn't mention: UIViewAnimationTransitionCurlDown and UIViewAnimationTransitionCurlUp.
CurlUp looks like someone is peeling the view from the bottom up to the top, and CurlDown looks like the view is being unrolled down on top of the screen from top to bottom. You should at least give them a look, as they are easy to use.