UICoordinateSpace
for UIImageView
image written in Swift 2
Convert CGPoint
from image coordinates to view coordinates:
import ImageCoordinateSpace
let imageSpace = imageView.contentSpace()
let imageTopLeft = imageSpace.convertPoint(CGPointZero, toCoordinateSpace: imageView)
Convert CGPoint
from view coordinates to image coordinates:
let viewTopLeft = imageSpace.convertPoint(CGPointZero, fromCoordinateSpace: imageView)
Similar conversions are available for CGRect
:
let imageRect = CGRect(x: 321, y: 102, width: 63, height: 64)
let viewRect = imageSpace.convertRect(imageRect, toCoordinateSpace: imageView)
and from view coordinates to image coordinates:
let viewRect = CGRect(x: 107, y: 68, width: 21, height: 42.667)
let imageRect = imageSpace.convertRect(viewRect, fromCoordinateSpace: imageView)
Open included Xcode Example project to see Xcode Playground: Example/Visual.playground
http://stackoverflow.com/questions/12201603/translating-cidetector-face-detection-results-into-uiimageview-coordinates http://stackoverflow.com/questions/33198266/convert-from-cirectanglefeature-coordinates-to-view-coordinates
###Other uses from popular Stackoverflow questions:
- Show image popover: http://stackoverflow.com/questions/15968251/get-the-zoomed-coordinates-of-an-image
- Reposition image: http://stackoverflow.com/questions/16733419/how-to-get-uiimage-coordinates-x-y-width-height-in-uiimageview-in-xcode
- Add touch events to image: http://stackoverflow.com/questions/17200191/converting-uiimageview-touch-coordinates-to-uiimage-coordinates
- Implement pan and zoom for the image: http://stackoverflow.com/questions/1813432/how-to-determine-image-coordinates-for-a-large-cropped-image-within-a-uiimagevie
HeaderDoc is included, see UIImageView+UICoordinateSpace.swift
- add the following line to your Podfile:
pod 'ImageCoordinateSpace'
- run
pod install
-
add to your Cartfile:
github "paulz/ImageCoordinateSpace"
-
run
carthage update
-
link your app with
ImageCoordinateSpace.framework
fromCarthage/Build/iOS
folder
To run included unit tests install dependencies via Carthage:
carthage bootstrap
Run tests:
xctool test
Paul Zabelin, https://github.com/paulz
ImageCoordinateSpace is available under the MIT license. See the LICENSE file for more info.