save image in core data

This commit is contained in:
Bob Sun 2017-02-09 14:41:59 +08:00
parent bcf4fd909e
commit e2036f202f
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
2 changed files with 10 additions and 2 deletions

View file

@ -58,6 +58,11 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
tableView.addSubview(indicator) tableView.addSubview(indicator)
tableView.addSubview(indicatorLable) tableView.addSubview(indicatorLable)
if let imageData = passwordEntity?.image {
let image = UIImage(data: imageData as Data)
passwordImage = image
}
DispatchQueue.global(qos: .userInitiated).async { DispatchQueue.global(qos: .userInitiated).async {
do { do {
self.password = try self.passwordEntity!.decrypt()! self.password = try self.passwordEntity!.decrypt()!
@ -90,7 +95,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
self?.tableView.reloadData() self?.tableView.reloadData()
indicator.stopAnimating() indicator.stopAnimating()
indicatorLable.isHidden = true indicatorLable.isHidden = true
if self?.password.url != "" { if self?.password.url != "" && self?.passwordEntity?.image == nil{
self?.updatePasswordImage(url: self?.password.url ?? "") self?.updatePasswordImage(url: self?.password.url ?? "")
} }
} }
@ -107,6 +112,8 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
self?.passwordImage = image self?.passwordImage = image
self?.tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic) self?.tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
print("success") print("success")
let imageData = UIImageJPEGRepresentation(image, 1)
self?.passwordEntity?.setValue(imageData, forKey: "image")
case .failure(let error): case .failure(let error):
print(error) print(error)
} }

View file

@ -6,6 +6,7 @@
<relationship name="password" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="PasswordEntity" inverseName="categories" inverseEntity="PasswordEntity" syncable="YES"/> <relationship name="password" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="PasswordEntity" inverseName="categories" inverseEntity="PasswordEntity" syncable="YES"/>
</entity> </entity>
<entity name="PasswordEntity" representedClassName="PasswordEntity" syncable="YES" codeGenerationType="class"> <entity name="PasswordEntity" representedClassName="PasswordEntity" syncable="YES" codeGenerationType="class">
<attribute name="image" optional="YES" attributeType="Binary" syncable="YES"/>
<attribute name="name" attributeType="String" syncable="YES"/> <attribute name="name" attributeType="String" syncable="YES"/>
<attribute name="raw" optional="YES" attributeType="Binary" syncable="YES"/> <attribute name="raw" optional="YES" attributeType="Binary" syncable="YES"/>
<attribute name="rawPath" attributeType="String" syncable="YES"/> <attribute name="rawPath" attributeType="String" syncable="YES"/>
@ -13,6 +14,6 @@
</entity> </entity>
<elements> <elements>
<element name="PasswordCategoryEntity" positionX="115" positionY="-9" width="128" height="90"/> <element name="PasswordCategoryEntity" positionX="115" positionY="-9" width="128" height="90"/>
<element name="PasswordEntity" positionX="-63" positionY="-18" width="128" height="105"/> <element name="PasswordEntity" positionX="-63" positionY="-18" width="128" height="120"/>
</elements> </elements>
</model> </model>