[default] [ERROR] Could not create a bookmark: NSError: Cocoa 257 “The file couldn’t be opened because you don’t have permission to view it.”

Environment

Swift
iOS 14.4
On a real iPhone 8 device

Code

   func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
        // close picker
        picker.dismiss(animated: true, completion: nil)
        // check canceled
        if results.count == 0 {
            debugPrint("PHImagePicker was canceled")
            return
        }
        if let itemProvider = results.first?.itemProvider,
           itemProvider.canLoadObject(ofClass: UIImage.self) {
            itemProvider.loadObject(ofClass: UIImage.self, completionHandler: {image, error in
                // do something.
            })
        }
    }

Symptom

When an image is selected by PHPicker, the following error message was reported.

2021-02-20 00:12:24.794146+0900 *** [6290:1858791] [default] [ERROR] Could not create a bookmark: NSError: Cocoa 257 “The file couldn’t be opened because you don’t have permission to view it.” }

Main Thread Checker: UI API called on a background thread: -[UIImageView setImage:]

Error Message #1

Could not create a bookmark: NSError: Cocoa 257 “The file couldn’t be opened because you don’t have permission to view it.”

Solution #1

So far, the code works for me as expected with the message. It seems the error message could be ignored.

Stay tuned with the Apple Developer Forum

Error Message #2

Main Thread Checker: UI API called on a background thread: -[UIImageView setImage:]

Cause #2

imageView has been accessed from the PHPicker completion handler as follows.

itemProvider.loadObject(ofClass: UIImage.self,
completionHandler: {image, error in
                     guard let image = image as? UIImage 

GUI operations should be called in the main thread.

Solution #2

In the completionHandler, image should be handled in the main thread.

   func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
        // close picker
        picker.dismiss(animated: true, completion: nil)
        // check canceled
        if results.count == 0 {
            debugPrint("PHImagePicker was canceled")
            return
        }
        if let itemProvider = results.first?.itemProvider,
           itemProvider.canLoadObject(ofClass: UIImage.self) {
            itemProvider.loadObject(ofClass: UIImage.self, completionHandler: {image, error in
                DispatchQueue.main.async {
                    guard let image = image as? UIImage else {
                        debugPrint("Error: UIImage is nil")
                        return }
                    self.imageView.image = image
                }
            })
        }
    }


投稿者: admin

Free Software Engineer

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です