JohnnyDark / IOS_MyLocation

0 stars 0 forks source link

图片的保存 #18

Open JohnnyDark opened 4 years ago

JohnnyDark commented 4 years ago
逻辑设置
  1. 属性设计 photoID: 添加到数据库中,存储属性

    @NSManaged public var photoID: NSNumber?
     // 通过类方法生成ID
    class func nextPhotoID() -> Int {
           let userDefaults = UserDefaults.standard
           let currentID = userDefaults.integer(forKey: "PhotoID") + 1
           userDefaults.set(currentID, forKey: "PhotoID")
          userDefaults.synchronize()
          return currentID
    }

hasPhoto: 通过是否有photoID判断 location对象是否有图片

var hasPhoto: Bool {
            return photoID != nil
         }

photoImage: 通过photoURL从保存路径URL中将图片对象读取到内存

  var photoImage: UIImage? {
             return UIImage(contentsOfFile: photoURL.path)
         }

photoURL: 通过photoID来生成唯一的图片保存路径

 var photoURL: URL {
               assert(photoID != nil, "No photo ID set")
               let filename = "Photo-\(photoID!.intValue).jpg"
               return applicationDocumentsDirectory.appendingPathComponent(
               filename)
      }
JohnnyDark commented 4 years ago
1. 图片保存逻辑
2. 图片显示逻辑