gpt4 book ai didi

Swift - 从核心数据中检索图像

转载 作者:行者123 更新时间:2023-11-28 09:58:38 26 4
gpt4 key购买 nike

我已经在互联网上(主要是在这里)搜索了我的问题,我已经尝试过了,但我仍然无法让它工作。我正在尝试从核心数据中检索图像,然后将其显示在表格 View 中。所以基本上,一个带有缩略图的表格 View 以及一个名称和一个开关。我在下面有以下 swift 文件。任何人都可以引导我做什么?除了图像检索之外,我还计划添加一个开关,当打开/关闭时它会要求输入密码,以便可以打开/关闭它。我该如何开始呢?希望得到您的友好回应。谢谢!

我有这段代码用于将包含图像的记录保存到核心数据。

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,它在 TableView 上显示记录,并且应该与图像一起显示,我正在尝试弄清楚。

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()
}

我认为我应该在此处的某处编写代码以从核心数据中检索图像并将其显示在 TableView 上,这样的想法是否正确?

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
}

最佳答案

好的,这是固定的代码:

TeachTableViewController:

//
// TEACHTableViewController.swift
// TEACH
//
// Created by ICST340.N1 on 9/29/15.
// Copyright © 2015 IyoTugs. All rights reserved.
//

import UIKit
import CoreData

class TEACHTableViewController: UITableViewController {
let managedObjectContext =
(UIApplication.sharedApplication().delegate
as! AppDelegate).managedObjectContext

var fetchedLastName = [String]()
var fetchedFirstName = [String]()
var fetchedImage = [UIImage]()

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)!)
//added this is convert the image
let image = match.valueForKey("image") as! NSData
fetchedImage.append(UIImage(data: image)!)

}
} else {

}
}
catch{}
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncommented this
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

func reloadData(notification: NSNotification){
fetchedLastName.removeAll()
fetchedFirstName.removeAll()
//Added this to remove all images
fetchedImage.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)!)
//added this is convert the image
let image = match.valueForKey("image") as! NSData
fetchedImage.append(UIImage(data: image)!)

}
} else {

}
}
catch{}

self.tableView.reloadData()
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return fetchedLastName.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TEACH", forIndexPath: indexPath) as! TEACHTableViewCell

let row = indexPath.row

print(fetchedFirstName)

cell.facultyName.text = fetchedLastName[row] + ", " + fetchedFirstName[row]
cell.facultyImage.image = fetchedImage[row]

return cell
}

}

我标记了我修复的每个点,但只有四个点。其中两个与获取 NSData 并将其转换为图像有关(在 viewDidLoad 和 reloadData 中,相同的位置)。其中之一是注释掉使右侧导航栏按钮成为编辑按钮的行。最后一个 fetchedImage.removeAll() 在 reloadData 的顶部取出所有图像。

关于Swift - 从核心数据中检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32869971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com