Image Coordinate Space
UIImageView
的 UICoordinateSpace
用法
将图像坐标中的 CGPoint
转换为视图坐标
import ImageCoordinateSpace
let imageSpace = imageView.contentSpace()
let imageTopLeft = imageSpace.convert(CGPoint.zero, to: imageView)
将视图坐标中的 CGPoint
转换为图像坐标
let viewTopLeft = imageSpace.convert(CGPoint.zero, from: imageView)
类似转换也适用于 CGRect
let imageRect = CGRect(x: 321, y: 102, width: 63, height: 64)
let viewRect = imageSpace.convert(imageRect, to: imageView)
以及从视图坐标到图像坐标的转换
let viewRect = CGRect(x: 107, y: 68, width: 21, height: 42.667)
let imageRect = imageSpace.convert(viewRect, from: imageView)
示例
添加定位图像覆盖层
打开包含的 Xcode 示例项目以查看 Xcode Playground:[Example/Visual.playground](https://github.com/paulz/ImageCoordinateSpace/blob/master/Example/Visual.playground)
将图像面部检测区域转换为视图触摸点
- 将CIDetector(面部检测)结果转换为UIImageView坐标 http://stackoverflow.com/questions/12201603/translating-cidetector-face-detection-results-into-uiimageview-coordinates
- 从CIRectangleFeature坐标转换为视图坐标 http://stackoverflow.com/questions/33198266/convert-from-cirectanglefeature-coordinates-to-view-coordinates
来自热门Stackoverflow问题的其他用途
- 显示图像弹出窗口: http://stackoverflow.com/questions/15968251/get-the-zoomed-coordinates-of-an-image
- 重新定位图像: http://stackoverflow.com/questions/16733419/how-to-get-uiimage-coordinates-x-y-width-height-in-uiimageview-in-xcode
- 向图像添加触摸事件: http://stackoverflow.com/questions/17200191/converting-uiimageview-touch-coordinates-to-uiimage-coordinates
- 实现图像的滚动和缩放: http://stackoverflow.com/questions/1813432/how-to-determine-image-coordinates-for-a-large-cropped-image-within-a-uiimagevie
文档
HeaderDoc包括,请参阅UIImageView+UICoordinateSpace.swift
安装
CocoaPods
- 在您的Podfile中添加以下行
pod 'ImageCoordinateSpace'
- 运行
pod install
Carthage
-
将其添加到您的Cartfile
github "paulz/ImageCoordinateSpace"
-
运行
carthage update
-
将您的应用程序与应用程序中的
ImageCoordinateSpace.framework
链接在一起,从Carthage/Build/iOS
文件夹中
单元测试
要运行包含的单元测试,请通过Carthage安装依赖
carthage bootstrap
运行测试
xcodebuild test -scheme ImageCoordinateSpace -destination 'platform=iOS Simulator,name=iPhone X'
闪电般快速通过
Test Suite 'All tests' passed at 2017-12-13 08:07:14.826.
Executed 36 tests, with 0 failures (0 unexpected) in 0.013 (0.023) seconds
** TEST SUCCEEDED **
作者
Paul Zabelin, https://github.com/paulz
许可协议
ImageCoordinateSpace 根据 MIT 许可协议提供。有关更多信息,请参阅 LICENSE 文件。