菜鸟教程小白 发表于 2022-12-11 20:27:52

ios - 存储对相机胶卷图像的引用 - Swift 4


                                            <p><p>我有一个非常简单的请求,显然超出了我的能力范围。我要做的就是允许用户从相机胶卷中选择一张图片,然后在我的应用程序中存储对该图片的“引用”。然后,我可以在需要时从相机胶卷中加载图像。</p>

<p>我不想复制图像并将其保存在其他地方,因为我觉得这会浪费手机空间。我意识到用户可以从他们的相机胶卷中删除图像,但在这个应用程序的情况下,这并不重要。我将在显示图像之前简单地检查 nil 。它不会影响应用程序的功能。</p>

<p>我可以展示 ImagePickerController,并且很高兴通过以下方式获得 UIImage:</p>

<pre><code>info
</code></pre>

<p>我想做的是使用:</p>

<pre><code>info
</code></pre>

<p>但是,此功能已被删除,不再受支持。因此,我在寻找 PHAsset 的过程中浪费了几个小时。如果我有要搜索的内容,这对于检索来说似乎很好。我似乎无法编写一个简单的应用程序来通过 UIImagePickerController 获取图像,然后允许我保存引用/URL/uniqueID 等,然后我可以通过其他方法再次使用它们来获取图像。</p>

<p>我很高兴被 mock ,只要笑的人向我展示了我是多么愚蠢……</p>

<p>非常感谢所有帮助。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我需要以编程方式检查 PHPhotoLibrary.authorizationStatus(),因为应用程序默默地失败了。将以下内容添加到 AppDelegate.swift 解决了该问题(您需要导入照片):</p>

<pre><code>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 &amp; GALLERY NOT ALLOWING ACCESS - ALERT
func alertToEncourageCameraAccessWhenApplicationStarts()
{
    //Camera not available - Alert
    let internetUnavailableAlertController = UIAlertController (title: &#34;Camera Unavailable&#34;, message: &#34;Please check to see if it is disconnected or in use by another application&#34;, preferredStyle: .alert)

    let settingsAction = UIAlertAction(title: &#34;Settings&#34;, style: .destructive) { (_) -&gt; 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: &#34;Okay&#34;, 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: &#34;Photo Library Unavailable&#34;, message: &#34;Please check to see if device settings doesn&#39;t allow photo library access&#34;, preferredStyle: .alert)

    let settingsAction = UIAlertAction(title: &#34;Settings&#34;, style: .destructive) { (_) -&gt; Void in
      let settingsUrl = NSURL(string:UIApplicationOpenSettingsURLString)
      if let url = settingsUrl {
            UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
      }
    }
    let cancelAction = UIAlertAction(title: &#34;Okay&#34;, style: .default, handler: nil)
    cameraUnavailableAlertController .addAction(settingsAction)
    cameraUnavailableAlertController .addAction(cancelAction)
    self.window?.rootViewController!.present(cameraUnavailableAlertController , animated: true, completion: nil)
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 存储对相机胶卷图像的引用 - Swift 4,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/51828609/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/51828609/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 存储对相机胶卷图像的引用 - Swift 4