如何从UICollectionViewCell获取第一个图像并保存在UITableViewCell

时间:2016-01-20 19:22:18

标签: swift uitableview core-data uiimage uicollectionview

我想从UICollectionView保存第一个或最后一个图像并将其保存在UITabelView Cell中作为UIImage.I尝试使用第一个indexPath if indexPath == 0捕获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
}

以下是UITableViewCell的代码:

 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
}

0 个答案:

没有答案