Ali Akbar
Ali Akbar

Reputation: 1

How to fix "This decoder will only decode classes that adopt NSSecureCoding. Class 'UIImageView' does not adopt it."

I am trying to archive my UIImageView which contains many CaLayers and Views.

I am successfully saved 'UIImageView' as Data through below code..

        let data = try? NSKeyedArchiver.archivedData(withRootObject: self.backgroundIV!, requiringSecureCoding: false)
        print(data)

When I try to un-archived UIImageView form Data via below code.

do {
            let imageView: UIImageView = try NSKeyedUnarchiver.unarchivedObject(ofClass: UIImageView.self, from: data!)!
            self.backgroundIV = imageView
            print("Un Archived...")
        } catch {
            print(error)
        }
   }

I got this error..

Optional(922 bytes)
Error Domain=NSCocoaErrorDomain Code=4864 "This decoder will only decode classes that adopt NSSecureCoding. Class 'UIImageView' does not adopt it." UserInfo={NSDebugDescription=This decoder will only decode classes that adopt NSSecureCoding. Class 'UIImageView' does not adopt it.
}

Upvotes: -1

Views: 641

Answers (1)

GingerBreadMane
GingerBreadMane

Reputation: 2697

Generally, I'd suggest archiving the input data of your UIImageView rather than the image view itself. Regardless, here's how to unarchive using NSKeyedUnarchiver with classes that don't adopt NSSecureCoding:

do {
    let data = try Data(contentsOf: someURL)
            
    let unarchiver = try NSKeyedUnarchiver(forReadingFrom: data)
    unarchiver.requiresSecureCoding = false
    let imageView = try unarchiver.decodeTopLevelObject(of: UIImageView.self, forKey: NSKeyedArchiveRootObjectKey)
    unarchiver.finishDecoding()

    return imageView
} catch {
    return nil
}

Upvotes: -1

Related Questions