【问题标题】:Using NSCollectionView with CoreData and an NSImageView将 NSCollectionView 与 CoreData 和 NSImageView 一起使用
【发布时间】:2016-01-03 13:59:39
【问题描述】:

我正在尝试设置一个绑定到实体的 NSCollectionView,并且我正在尝试将图像加载到该实体之外的 NSImageView 中。我将图像存储为 NSData 对象。
我做了一些研究,发现我应该设置一个 ValueTransformer 但我无法完成这项工作。

这是我的图像属性pdfImage的保存过程的代码:

pdf = NSData(contentsOfFile: path)

            // pdf to image
            let pdfImageRep = NSPDFImageRep(data: pdf)
            let factor: CGFloat = 300/72

            pdfImageRep?.currentPage = 1
            let image = NSImage(size: self.frame.size, flipped: false,
                drawingHandler: {
                    dstRect in

                    (pdfImageRep?.drawInRect(dstRect))!
                    return true
            })

            let scaledImageRep = image.representations.first
            scaledImageRep?.pixelsWide = Int((pdfImageRep?.size.width)! * factor)
            scaledImageRep?.pixelsHigh = Int((pdfImageRep?.size.height)! * factor)

            let pngImageRep = NSBitmapImageRep(data: image.TIFFRepresentation!)
            let imageData = pngImageRep?.representationUsingType(NSBitmapImageFileType.NSJPEGFileType, properties: [:])

            pdfImage = imageData

我稍后在代码中保存 pdfImage。

正如我所说,我尝试使用 NSKeyedUnarchiveFromData 作为 ValueTransformer,但没有成功。

是我做错了还是有更简单的方法?有人可以帮我解决这个问题吗?

非常感谢!

【问题讨论】:

    标签: macos cocoa core-data nscollectionview nsimageview


    【解决方案1】:

    我将 NSImage 存储在 Core Data 中。数据模型中的类型是可转换的。

    anImage = [[NSImage alloc] initWithContentsOfURL:aFileURL];
    if (anImage) {
        anImageObject = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:[self managedObjectContext]];
        [anImageObject setImage:anImage];
    }
    

    我在没有值转换器的情况下将 NSImageView 的值绑定到图像属性。

    ps。如果您在 Core Data 中存储图像数据(或其他 blob),请将图像放在单独的实体中或存储路径中。请参阅blob section of the Core Data Programming Guide

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-08
      • 2017-05-16
      • 1970-01-01
      相关资源
      最近更新 更多