我有一个非常简单的请求,显然超出了我的范围。我要做的就是允许用户从相机胶卷中选择一张图片,然后将对该图片的“引用”存储在我的应用中。然后,我可以在需要时从相机胶卷加载图像。
我不想复制图像并将其保存在其他位置,因为我认为这会浪费手机上的空间。我意识到用户可以从他们的相机胶卷中删除图像,但是对于这个应用程序,这并不重要。我只是在显示图像之前检查nil。不会影响应用功能。
我可以展示ImagePickerController,并且很高兴通过以下方式获取UIImage:
info[UIImagePickerControllerOriginalImage]
我想做的是使用:
info[UIImagePickerControllerReferenceURL]
但是,此内容已被删除,不再受支持。因此,我失去了数小时来寻找PHAsset的生命。只要我有要搜索的内容,这对于检索来说似乎很好。我似乎无法编写一个简单的应用程序来通过UIImagePickerController获取图像,然后允许我保存引用/ URL / uniqueID等,然后可以通过其他方法使用该应用程序以再次获取图像。
我很高兴被人嘲笑,只要那个笑的人向我展示我一直有多愚蠢……
非常感谢所有帮助。
答案 0 :(得分:0)
我需要以编程方式检查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: "Please 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: "Photo Library Unavailable", message: "Please 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)
}