Swift 如何从UICollectionViewCell获取第一个图像并保存在UITableViewCell中
我想保存UICollectionView中的第一个或最后一个图像,并将其作为UIImage保存在UITableView单元格中。如果indexPath==0,则使用第一个indexPathSwift 如何从UICollectionViewCell获取第一个图像并保存在UITableViewCell中,swift,uitableview,core-data,uiimage,uicollectionview,Swift,Uitableview,Core Data,Uiimage,Uicollectionview,我想保存UICollectionView中的第一个或最后一个图像,并将其作为UIImage保存在UITableView单元格中。如果indexPath==0,则使用第一个indexPath尝试获取UIImage,然后将其作为NSData保存在CoreData中,但它不起作用。有没有人知道如何在UITableViewCell中显示UIImage?谢谢你的帮助 以下是UICollectionViewCell中的代码: override func collectionView(collectionVi
尝试获取UIImage,然后将其作为NSData保存在CoreData中,但它不起作用。有没有人知道如何在UITableViewCell中显示UIImage?谢谢你的帮助
以下是UICollectionViewCell中的代码:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("BookPicCell", forIndexPath: indexPath) as! BookPicCell
let cellcore = picture[indexPath.row]
cell.BookImage.image = UIImage(contentsOfFile: cellcore.foto!)
if indexPath.row == 0 {
let firstpic = cell.BookImage.image
let firstcell = UIImagePNGRepresentation(firstpic!)
let firstimag = NSEntityDescription.insertNewObjectForEntityForName("Book", inManagedObjectContext: self.mgdContext) as! Book
firstimag.firstimage = firstcell!
}
do {
try self.mgdContext.save()
} catch {
print("Error")
}
return cell
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let Cell = BookTableView.dequeueReusableCellWithIdentifier("BookCell", forIndexPath: indexPath) as! BookCell
let books = book[indexPath.row]
Cell.BookTitle.text = books.title
let picturecount = books.picture!.count
Cell.BookImageCount.text = "\(picturecount) Picture"
let firstpicture = books.firstimage!
Cell.BookImage.image = UIImage(data: firstpicture)
return Cell
}
以下是UITableViewCell中的代码:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("BookPicCell", forIndexPath: indexPath) as! BookPicCell
let cellcore = picture[indexPath.row]
cell.BookImage.image = UIImage(contentsOfFile: cellcore.foto!)
if indexPath.row == 0 {
let firstpic = cell.BookImage.image
let firstcell = UIImagePNGRepresentation(firstpic!)
let firstimag = NSEntityDescription.insertNewObjectForEntityForName("Book", inManagedObjectContext: self.mgdContext) as! Book
firstimag.firstimage = firstcell!
}
do {
try self.mgdContext.save()
} catch {
print("Error")
}
return cell
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let Cell = BookTableView.dequeueReusableCellWithIdentifier("BookCell", forIndexPath: indexPath) as! BookCell
let books = book[indexPath.row]
Cell.BookTitle.text = books.title
let picturecount = books.picture!.count
Cell.BookImageCount.text = "\(picturecount) Picture"
let firstpicture = books.firstimage!
Cell.BookImage.image = UIImage(data: firstpicture)
return Cell
}
“它不起作用”并不能描述你遇到的问题。这一页可能会有帮助:我想知道这段代码是否与indexPath==0
一起工作,以捕获单元格的第一个图像,或者让任何人知道如何从单元格中获取第一个图像。你知道我可以在那里做些什么来获取图像吗?