Skip to content

Instantly share code, notes, and snippets.

@sgss
Created February 3, 2013 18:14
Show Gist options
  • Save sgss/4702906 to your computer and use it in GitHub Desktop.
Save sgss/4702906 to your computer and use it in GitHub Desktop.
- (CGPoint)pointForPointOfInterest:(CGPoint)pointOfInterest
{
CGPoint point;
if ([previewLayer respondsToSelector:
@selector(pointForCaptureDevicePointOfInterest:)]) {
// Use the native method if available (available on iOS 6 or above.)
point = [previewLayer pointForCaptureDevicePointOfInterest:pointOfInterest];
} else {
// Otherwise manually convert the point from point-of-interest coordinates
// to preview layer coordinates. Notice we set the content gravity of the
// preview layer to AVLayerVideoGravityResizeAspectFill.
CGRect bounds = [previewLayer bounds];
CGFloat width = CGRectGetHeight(bounds);
CGFloat height = CGRectGetWidth(bounds);
CGFloat aspectRatio = inputSize.width / inputSize.height;
if (width / height > aspectRatio) {
point.x = (pointOfInterest.y - 0.5) * width * aspectRatio - height / 2.0;
point.y = pointOfInterest.x * width;
} else {
point.x = (1.0 - pointOfInterest.y) * height;
point.y = (0.5 - pointOfInterest.x) * height / aspectRatio - width / 2.0;
}
}
return point;
}
- (CGPoint)pointOfInterestForPoint:(CGPoint)point
{
// Same discussion above.
CGPoint pointOfInterest;
if ([previewLayer respondsToSelector:
@selector(captureDevicePointOfInterestForPoint:)]) {
pointOfInterest = [previewLayer captureDevicePointOfInterestForPoint:point];
} else {
CGRect bounds = [previewLayer bounds];
CGFloat width = CGRectGetHeight(bounds);
CGFloat height = CGRectGetWidth(bounds);
CGFloat aspectRatio = inputSize.width / inputSize.height;
if (width / height > aspectRatio) {
pointOfInterest.x = point.y / width;
pointOfInterest.y = 0.5 + (height / 2.0 + point.x) / width / aspectRatio;
} else {
pointOfInterest.x = 0.5 - (width / 2.0 + point.y) / height * aspectRatio;
pointOfInterest.y = 1.0 - point.x / height;
}
}
return pointOfInterest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment