我通过互联网(主要是在这里)搜索了我的问题并且我已经尝试了但是我仍然无法使用它。我正在尝试从核心数据中检索图像,然后将其显示在表格视图中。所以基本上是一个包含缩略图图像以及名称和开关的表格视图。我在下面有以下快速文件。任何人都可以带我做什么?除了图像检索之外,我打算添加一个开关,当打开/关闭它时会要求输入密码,以便可以打开/关闭它。我怎么开始呢?希望你的回应。谢谢!
我有这段代码用于保存包含图像到核心数据的记录。
let mOC =
(UIApplication.sharedApplication().delegate
as! AppDelegate).managedObjectContext
@IBOutlet weak var firstName: UITextField!
@IBOutlet weak var lastName: UITextField!
@IBOutlet weak var status: UILabel!
@IBAction func saveRecord(sender: AnyObject) {
let entityDescription =
NSEntityDescription.entityForName("Faculty",
inManagedObjectContext: mOC)
let faculty = Faculty(entity: entityDescription!,
insertIntoManagedObjectContext: mOC)
faculty.firstname = firstName.text
faculty.lastname = lastName.text
let pickedImage = UIImageJPEGRepresentation(imageView.image!, 1.0)
faculty.image = pickedImage
do {
try mOC.save()
firstName.text = ""
lastName.text = ""
status.text = "New record saved."
NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
self.dismissViewControllerAnimated(true, completion: nil)
} catch {
status.text = "Record not saved."
}
}
@IBOutlet weak var imageView: UIImageView!
var newMedia : Bool?
@IBAction func choosePhoto(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(
UIImagePickerControllerSourceType.SavedPhotosAlbum) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType =
UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true,
completion: nil)
newMedia = false
}
}
这是TableViewController,它在表格视图上显示记录,据说与图像一起显示,我正试图弄清楚。
let managedObjectContext =
(UIApplication.sharedApplication().delegate
as! AppDelegate).managedObjectContext
var fetchedLastName = [String]()
var fetchedFirstName = [String]()
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadData:",name:"load", object: nil)
let entityDescription =
NSEntityDescription.entityForName("Faculty",
inManagedObjectContext: managedObjectContext)
let request = NSFetchRequest()
request.entity = entityDescription
do{
let objects = try managedObjectContext.executeFetchRequest(request)
let results = objects
if results.count > 0 {
for var i = 0; i < results.count; i += 1{
let match = results[i] as! NSManagedObject
fetchedLastName.append((match.valueForKey("lastname") as? String)!)
fetchedFirstName.append((match.valueForKey("firstname") as? String)!)
}
} else {
}
}
catch{}
}
func reloadData(notification: NSNotification){
fetchedLastName.removeAll()
fetchedFirstName.removeAll()
let entityDescription =
NSEntityDescription.entityForName("Faculty",
inManagedObjectContext: managedObjectContext)
let request = NSFetchRequest()
request.entity = entityDescription
do{
let objects = try managedObjectContext.executeFetchRequest(request)
let results = objects
if results.count > 0 {
for var i = 0; i < results.count; i += 1{
let match = results[i] as! NSManagedObject
fetchedLastName.append((match.valueForKey("lastname") as? String)!)
fetchedFirstName.append((match.valueForKey("firstname") as? String)!)
}
} else {
}
}
catch{}
self.tableView.reloadData()
}
我是否认为我应该编写代码以从核心数据中检索图像并将其显示在表格视图上,这是正确的。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TEACH", forIndexPath: indexPath) as! TEACHTableViewCell
let row = indexPath.row
cell.facultyName.text = fetchedLastName[row] + ", " + fetchedFirstName[row]
return cell
}
答案 0 :(得分:3)
好的,这里是固定代码:
TeachTableViewController:
ComboBox
我标记了我修复的每个点,但只有四个点。其中两个与获取NSData并将其转换为Image(在viewDidLoad和reloadData中,相同的点)有关。其中一个添加了注释掉使右侧导航按钮成为编辑按钮的行。最后一个在reloadData顶部的fetchedImage.removeAll()取出所有图像。
答案 1 :(得分:1)
不完全确定Faculty()包含但是,您需要将图像作为NSData保存到CoreData,然后当您想从CoreData获取图像时,您将获取NSData并将其转换为如下图像:
let image = UIImage(named: "BackgroundSky")
//Change named: "" to your image
let imageAsNSData = UIImageJPEGRepresentation(image!, 1.0)
然后将它作为NSData保存到CoreData,然后当你从CoreData获得它时,这就是你把它变成图像的方式:
let image = UIImage(data: imageAsNSData!)
//Change data: to the NSData
这就是从UIImage到NSData的来回变换和图像。
答案 2 :(得分:0)
解决这个问题:
var fetchedImage = [NSData]()
fetchedImage.append((match.valueForKey("image") as? NSData)!)
和
cell.facultyImage.image = fetchedImage[row]