这个 Swift 包将 UIImagePickerController 集成到 SwiftUI 应用程序中,并允许用户选择、缩放和定位图像以进行裁剪,并将其保存为联系人的照片,类似于 iOS 自带的“通讯录”应用程序。为了实现这一点,该项目使用 composeImageAttributes()
函数,通过绑定将四个变量发送到父视图
UIImage
的副本,UIImage
版本,CGFloat
,以及CGPoint
。CGFloat
和 CGPoint
表示用于生成处理后版本的原始图像的缩放和位置。
☝️ 通过保存原始图像以及缩放和位置标记,并将它们传递给
ImageMoveAndScaleSheet
视图,如果用户点击“更改照片”按钮,原始照片将被缩放和定位,以便进行后续调整。
像这样将 ImagePane 视图添加到您的父视图:
ImagePane(image: image, isEditMode: $isEditMode, renderingMode: renderingMode, colors: colors)
使用 CoreData,可以执行以下操作。定义两个实体:Contact
和 ProfileImage
,其中 Contact
与 ProfileImage
具有“一对一”关系。ProfileImage
实体应具有可以映射到用户从其图库中选择图像后提供给 ImageDisplay
结构体的变量的属性。
然后,应使用 Core Data 实体的属性初始化 ImageAttributes
类。然后,将此 ImageAttributes
类传递给 ImagePane
,如上面的代码所示。对于 nil 情况,将初始化第二个 ImageAttributes
结构体,这次使用名为 avatar
的默认图像,该图像已保存到项目的 Image Assets 文件夹中。
ImageAttributes
对象可以轻松地定义为占位符。
使用 SF Symbol:let placeholderAperture = ImageAttributes(withSFSymbol: "camera.aperture")
使用 Assets 文件夹中的图像:let placeholderAvatar = ImageAttributes(withImage: "avatar")
使用此软件包的工作应用程序示例可以在以下位置找到:https://github.com/Rillieux/Contacts(使用 CoreData),以及 https://github.com/Rillieux/PhotoSelectAndCropDemo(一个基本的工作实现,不包括持久性)。