最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[基本解决]swift中UIImage用NSCoder执行decodeObjectForKey解码出错

Swift crifan 2331浏览 0评论

代码:

    override init(){
        id = ""
        md5 = ""
        type = FileType.Other
        name = ""
        suffix = ""
        senderName = ""
        sizeInBytes = 0
        fromSource = ""
        updatedTime = NSDate()
        createdTime = NSDate()
        iconImage = UIImage(named: "file_icon_other")!
        iconImageLarge = UIImage(named: "file_icon_large_other")!
    }
required init?(coder aDecoder: NSCoder){
    self.iconImage = aDecoder.decodeObjectForKey(FileItem_iconImage) as! UIImage
}

搜:

swift UIImage decodeObjectForKey fail

ios – Shows an error Non – fail able initializer "init(coder:)" error – Stack Overflow

ios – Swift: decodeObjectForKey crashes if key doesn’t exist – Stack Overflow

Ray Wenderlich | Tutorials for Developers and Gamers

Using NSCoder with UIImage:swift

(已完结)马上着手开发 iOS 应用程序 (十一) – 持久化数据 – 简书

中也还是用的:

aCoder.encodeObject(photo, forKey: PropertyKey.photoKey)
let photo = aDecoder.decodeObjectForKey(PropertyKey.photoKey) as? UIImage

啊。。。

之前看了UIImage的定义:

@available(iOS 2.0, *)
public class UIImage : NSObject, NSSecureCoding {
}
public protocol NSSecureCoding : NSCoding {
   
    // This method must be return YES on all classes that allow secure coding. Subclasses of classes that adopt NSSecureCoding and override initWithCoder: must also override this method and return YES.
    // The Secure Coding Guide should be consulted when writing methods that decode data.
    public static func supportsSecureCoding() -> Bool
}

swift UIImage decodeObjectForKey

iphone – NSCoding Persisting UIImage – Stack Overflow

ios – NSKeyedArchiver: key returning nil – Swift – Stack Overflow

然后改为:

    required init?(coder aDecoder: NSCoder){
//        self.iconImage = aDecoder.decodeObjectForKey(FileItem_iconImage) as! UIImage
//        self.iconImageLarge = aDecoder.decodeObjectForKey(FileItem_iconImageLarge) as! UIImage
        let iconImageData = aDecoder.decodeObjectForKey(FileItem_iconImage)
        self.iconImage UIImage(data: iconImageData as! NSData)!
        let iconImageLargeData = aDecoder.decodeObjectForKey(FileItem_iconImageLarge)
        self.iconImageLarge UIImage(data: iconImageLargeData as! NSData)!
        super.init()
    }
   
    func encodeWithCoder(aCoder: NSCoder){
//        aCoder.encodeObject(self.iconImage, forKey: FileItem_iconImage)
//        aCoder.encodeObject(self.iconImageLarge, forKey: FileItem_iconImageLarge)
        let iconImageData = UIImagePNGRepresentation(self.iconImage)
        aCoder.encodeObject(iconImageData, forKey: FileItem_iconImage)
        let iconImageLargeData = UIImagePNGRepresentation(self.iconImageLarge)
        aCoder.encodeObject(iconImageLargeData, forKey: FileItem_iconImageLarge)
    }

就又可以了。

[总结]

之前很多的UIImage图片,都是直接用encodeObject和decodeObjectForKey:

比如:

        self.senderAvatar = aDecoder.decodeObjectForKey(Message_senderAvatar) as! UIImage
        self.senderAvatarLarge = aDecoder.decodeObjectForKey(Message_senderAvatarLarge) as! UIImage
        aCoder.encodeObject(self.senderAvatar, forKey: Message_senderAvatar)
        aCoder.encodeObject(self.senderAvatarLarge, forKey: Message_senderAvatarLarge)

也没有问题。

不知道为何此处有问题。。。

然后改为:

    required init?(coder aDecoder: NSCoder){
//        self.iconImage = aDecoder.decodeObjectForKey(FileItem_iconImage) as! UIImage
//        self.iconImageLarge = aDecoder.decodeObjectForKey(FileItem_iconImageLarge) as! UIImage
        let iconImageData = aDecoder.decodeObjectForKey(FileItem_iconImage)
        self.iconImage UIImage(data: iconImageData as! NSData)!
        let iconImageLargeData = aDecoder.decodeObjectForKey(FileItem_iconImageLarge)
        self.iconImageLarge UIImage(data: iconImageLargeData as! NSData)!
        super.init()
    }
   
    func encodeWithCoder(aCoder: NSCoder){
//        aCoder.encodeObject(self.iconImage, forKey: FileItem_iconImage)
//        aCoder.encodeObject(self.iconImageLarge, forKey: FileItem_iconImageLarge)
        let iconImageData = UIImagePNGRepresentation(self.iconImage)
        aCoder.encodeObject(iconImageData, forKey: FileItem_iconImage)
        let iconImageLargeData = UIImagePNGRepresentation(self.iconImageLarge)
        aCoder.encodeObject(iconImageLargeData, forKey: FileItem_iconImageLarge)
    }

即可。

根本原因,抽空再深究。

转载请注明:在路上 » [基本解决]swift中UIImage用NSCoder执行decodeObjectForKey解码出错

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
91 queries in 0.204 seconds, using 22.13MB memory