0

I'm trying to get the UIImage from the local URL, but it doesn't work. My someURl, where data is NSSecureCoding?: file:///Users/a19468455/Library/Developer/CoreSimulator/Devices/2FA5B725-9266-429C-A036-256D846BC3FA/data/Media/PhotoData/OutgoingTemp/3F73D172-AA12-40AD-A3AB-540543A43330/IMG_0004.JPG

Here's what I've tried (sacrificed code styling for convenience):

guard let inputItems = extensionContext?.inputItems as? [NSExtensionItem else { return }

inputItems.forEach { item in
            if let attachments = item.attachments,
               !attachments.isEmpty {
                attachments.forEach { attachment in
                    
                    if attachment.hasItemConformingToTypeIdentifier(kUTTypeImage as String) {
                        var image: UIImage?
                        
                        attachment.loadItem(forTypeIdentifier: kUTTypeImage as String, options: nil) { [weak self] data, error in
                            guard let self = self else { return }
                            guard error == nil else {
                                self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
                                return
                            }

                            if let someURl = data as? URL {
                                do {
                                    let data = try Data(contentsOf: someURl)
                                    image = UIImage(data: data)
                                } catch {
                                    print("failed to retrieve data")
                                }
                            }
                            if let image = image {
                                images.append(image)
                            }
                        }
                    }
                }
            }
        }

I also tried tips from here without success:

how to load image from local path ios swift (by path)

how to load image from local image path Ios Swift 4

Loading/Downloading image from URL on Swift

I have no way to get Data from my link, how do I fix it?

3
  • So what is the type of someURl? This line (if let someURl = data as? URL) looks pretty suspicious. Commented Mar 17, 2022 at 18:44
  • What do you mean “data is NSSecureCoding“? First of all NSSecureCoding is a protocol and secondly how do you expect casting this to a URL could work? Commented Mar 17, 2022 at 19:59
  • Yes, I clarified the question Commented Mar 18, 2022 at 10:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.