我有一个非常简单的请求,显然超出了我的能力范围。我要做的就是允许用户从相机胶卷中选择一张图片,然后在我的应用程序中存储对该图片的“引用”。然后,我可以在需要时从相机胶卷中加载图像。
我不想复制图像并将其保存在其他地方,因为我觉得这会浪费手机空间。我意识到用户可以从他们的相机胶卷中删除图像,但在这个应用程序的情况下,这并不重要。我将在显示图像之前简单地检查 nil 。它不会影响应用程序的功能。
我可以展示 ImagePickerController,并且很高兴通过以下方式获得 UIImage:
info[UIImagePickerControllerOriginalImage]
我想做的是使用:
info[UIImagePickerControllerReferenceURL]
但是,此功能已被删除,不再受支持。因此,我在寻找 PHAsset 的过程中浪费了几个小时。如果我有要搜索的内容,这对于检索来说似乎很好。我似乎无法编写一个简单的应用程序来通过 UIImagePickerController 获取图像,然后允许我保存引用/URL/uniqueID 等,然后我可以通过其他方法再次使用它们来获取图像。
我很高兴被 mock ,只要笑的人向我展示了我是多么愚蠢……
非常感谢所有帮助。
Best Answer-推荐答案 strong>
我需要以编程方式检查 PHPhotoLibrary.authorizationStatus(),因为应用程序默默地失败了。将以下内容添加到 AppDelegate.swift 解决了该问题(您需要导入照片):
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
photoLibraryAvailabilityCheck()
}
//MARK:- PHOTO LIBRARY ACCESS CHECK
func photoLibraryAvailabilityCheck()
{
if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.authorized
{
}
else
{
PHPhotoLibrary.requestAuthorization(requestAuthorizationHandler)
}
}
func requestAuthorizationHandler(status: PHAuthorizationStatus)
{
if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.authorized
{
}
else
{
alertToEncouragePhotoLibraryAccessWhenApplicationStarts()
}
}
//MARK:- CAMERA & GALLERY NOT ALLOWING ACCESS - ALERT
func alertToEncourageCameraAccessWhenApplicationStarts()
{
//Camera not available - Alert
let internetUnavailableAlertController = UIAlertController (title: "Camera Unavailable", message: "lease check to see if it is disconnected or in use by another application", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .destructive) { (_) -> Void in
let settingsUrl = NSURL(string:UIApplicationOpenSettingsURLString)
if let url = settingsUrl {
DispatchQueue.main.async {
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil) //(url as URL)
}
}
}
let cancelAction = UIAlertAction(title: "Okay", style: .default, handler: nil)
internetUnavailableAlertController .addAction(settingsAction)
internetUnavailableAlertController .addAction(cancelAction)
self.window?.rootViewController!.present(internetUnavailableAlertController , animated: true, completion: nil)
}
func alertToEncouragePhotoLibraryAccessWhenApplicationStarts()
{
//Photo Library not available - Alert
let cameraUnavailableAlertController = UIAlertController (title: "hoto Library Unavailable", message: "lease check to see if device settings doesn't allow photo library access", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .destructive) { (_) -> Void in
let settingsUrl = NSURL(string:UIApplicationOpenSettingsURLString)
if let url = settingsUrl {
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
}
}
let cancelAction = UIAlertAction(title: "Okay", style: .default, handler: nil)
cameraUnavailableAlertController .addAction(settingsAction)
cameraUnavailableAlertController .addAction(cancelAction)
self.window?.rootViewController!.present(cameraUnavailableAlertController , animated: true, completion: nil)
}
关于ios - 存储对相机胶卷图像的引用 - Swift 4,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/51828609/
|