我在视图控制器中为用户设置了一个注册表,用户在其中设置个人资料图像,还有一个带有“我的个人资料”标签的标签栏控制器,该图像在注册后应显示在其中。如何将图像从注册表单传输到选项卡控制器“我的个人资料”?从addPhotoImage(视图控制器)到userPhotoImage(选项卡视图控制器)。
const inputs = [
'220千伏',
'220千吨',
'220千',
];
for (const str of inputs) {
console.log(
str + (
/^\d+(?:,\d{3})*(?:\.\d+)?[ ]?千(?![伏吨])/.test(str)
? ' (match)'
: ' (no match)'
)
);
}
答案 0 :(得分:1)
首先。您要传输UIImage
,而不是UIImageView
。
UIImage
是图像的数据表示。
UIImage
仅显示图像。
您可以像这样使用单例:
class UserManager {
static let shared = UserManager()
var image: UIImage?
}
然后您可以从任何地方访问它
UserManager.shared.image
请注意,使用这种方法时,图像将在应用程序在内存中的同时保留在内存中。因此,仅当您一直需要照片时才使用它。
答案 1 :(得分:1)
如果要将图像存储在内存中,则fl034的解决方案效果很好。如果您希望将图像存储在永久性存储中,以便无论重启等情况都可用,则以下解决方案会将图像存储在文档目录中,以后可以访问:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
addPhotoImage.image = image
// Choose where to save the image, use a unique identifier to make it
// user specific
let uniqueIdentifier = someUniqueUserIdentifier
let fileName = getDocumentDirectory().appendingPathComponent("\(uniqueIdentifier)_profileImage")
// Write the image to the Document Directory
do {
try image.write(to: fileName)
} catch let error as NSError {
// Handle any errors
print("ERROR:", error)
return
}
dismiss(animated: true, completion: nil)
}
然后,当您要从文档目录中加载图像并将其分配给UIImageView进行演示时:
func loadProfileImageFromDirectory(uniqueIdentifier: String) {
// Dynamically select appropriate filepath
let fileName = getDocumentDirectory().appendingPathComponent("\(uniqueIdentifier)_profileImage")
guard let data = try? Data.init(contentsOf: fileName) else { return }
let loadedImage = UIImage(data: data)
// Set image view
someImageView.image = loadedImage
}