我的应用程序(swift、OSX、Xcode,所有的最新版本)都有一个 NSTextView,我允许用户在其中输入内容。 NSTextView 启用了 RichText、图形和 NSInspectorBar。
我将 NSTextView 的内容加载/保存为 webarchive 格式(咕噜咕噜——但我需要一种包含图像的可移植格式)。
到目前为止一切顺利。一切正常......除了一个小细节。当我将图像插入 TextView 时,它没有显示。但它就在那里,因为如果我保存并加载...它就会出现。
这是我将图像插入 NSTextView 的方法(我从 NSOpenPanel 获取图像的路径:
guard let theImage = NSImage(contentsOfURL: openPanel.URL!) else {
showErrorMessage("Unable to load image")
return
}
// self.textEditor is the NSTextView
guard let store = self.textEditor.textStorage else { abort() }
let attachment = NSTextAttachment()
attachment.image = theImage
let attrString = NSAttributedString(attachment: attachment)
let range = self.textEditor.selectedRange()
store.replaceCharactersInRange(range, withAttributedString: attrString)
我认为它相当简单:加载图像,添加到 attributedString,用 attributedString 替换所选字符。
但是什么也没有出现。 NSTextView 完全没有变化。如果我将 NSTextStorage 内容保存为网络存档:
guard let store = textEditor.textStorage else { abort() }
let attr: [String: AnyObject] = [NSDocumentTypeDocumentAttribute: NSWebArchiveTextDocumentType]
let data = try store.dataFromRange(NSMakeRange(0, store.length), documentAttributes: attire)
//... save 'data' to DB ...
然后重新加载它们:
//... load 'data' from the DB ...
guard let store = textEditor.textStorage else { abort() }
let str = try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSWebArchiveTextDocumentType], documentAttributes: nil)
store.setAttributedString(str)
...图像出现在正确的位置。
我对这个很困惑。有人对我缺少的东西有任何想法吗?
顺便说一句 - 我尝试使用以下方法强制刷新:
let frame = self.textEditor.frame
self.textEditor.setNeedsDisplayInRect(frame)
那没有用。
ADV 感谢您提供的任何建议。 干杯。 保罗
编辑:附录...我一直在搞乱 NSTextAttachmentCells,希望它们可以工作。它们以完全不同的方式失败。
如果我只是将上面的图像插入代码替换为:
let cell = NSTextAttachmentCell(imageCell: theImage)
let txtAtt = NSTextAttachment()
txtAtt.attachmentCell = cell
let str = NSAttributedString(attachment: txtAtt)
let range = self.textEditor.selectedRange()
store.replaceCharactersInRange(range, withAttributedString: str)
...然后图像会在插入后立即出现,并且出现在正确的位置。但他们没有得救!也就是说,当我保存并重新加载时(按照上面的代码),它们不会包含在 webarchive 中。
编辑^2: 因此,从上面得出的明显结论是,将图像附加到 TextAttachment 和 将图像附加到 NSTextAttachmentCell,NSTextAttachmentCell 也附加到 TextAttachment。好吧……那是行不通的。但是起作用的是:
let cell = NSTextAttachmentCell(imageCell: theImage)
let txtAtt = NSTextAttachment(data: theImage.TIFFRepresentation, ofType: kUTTypeTIFF as String)
txtAtt.attachmentCell = cell
//txtAtt.image = theImage
let str = NSAttributedString(attachment: txtAtt)
let rng = self.textEditor.selectedRange()
store.replaceCharactersInRange(rng, withAttributedString: str)
也就是说...我创建了一个 TextAttachment,传递图像的 TIFF 表示形式的数据。我还创建了 AttachmentCell。这现在有效 - 插入可见并保存。
唯一的缺点是 TIFF 非常大 - 图像扩展到内存大小的 3 倍。
任何人都可以给出一些关于为什么这有效的想法吗?
最佳答案
好的 - 这是解决方案。
NSTextAttachmentCell 代表
...the interface for objects that draw text attachment icons and handle mouse events on their icons apple docs.
因此您需要使用图像创建其中一个。
NSTextAttachment
...contains either an NSData object or an NSFileWrapper object, which in turn holds the contents of the attached file apple docs
因此您需要两者 - 一个用于显示和交互,一个用于保存(即实际数据)。我在保存 TIFF(太大)时遇到的问题可以通过创建 JPG 或 PNG 表示来解决,它们可以非常愉快地用于 NSData 对象。我通过扩展 NSImage 做到了这一点:
extension NSImage {
var imagePNGRepresentation: NSData {
return NSBitmapImageRep(data: TIFFRepresentation!)!.representationUsingType(.NSPNGFileType, properties: [:])!
}
var imageJPGRepresentation: NSData {
return NSBitmapImageRep(data: TIFFRepresentation!)!.representationUsingType(.NSJPEGFileType, properties: [:])!
}
}
(感谢来自 here 的评论者关于如何做到这一点)。
所以这里是可以实际将图像插入 NSTextView 的代码(请击鼓)...
guard let theImage = NSImage(contentsOfURL: openPanel.URL!) else {
showErrorMessage("Unable to load image")
return
}
guard let store = self.textEditor.textStorage else { abort() }
let cell = NSTextAttachmentCell(imageCell: theImage)
let txtAtt = NSTextAttachment(data: theImage.imageJPGRepresentation, ofType: kUTTypeJPEG as String)
txtAtt.attachmentCell = cell
let str = NSAttributedString(attachment: txtAtt)
let range = self.textEditor.selectedRange()
store.replaceCharactersInRange(range, withAttributedString: str)
当我将其保存为网络存档时,我可以看到 JPG 对象与 HTML 一起保存。
扩展此...您可能可以插入任何数据类型(例如文档)并在单元格中使用图标表示。很酷。
奇怪的是,大多数描述此内容的网页都不包含 Cell。老实说,我不知道它是如何工作的。
干杯。 保罗。
关于swift - 插入图像后 NSTextView 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37457558/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司
我正在尝试使用Ruby2.0.0和Rails4.0.0提供的API从imgur中提取图像。我已尝试按照Ruby2.0.0文档中列出的各种方式构建http请求,但均无济于事。代码如下:require'net/http'require'net/https'defimgurheaders={"Authorization"=>"Client-ID"+my_client_id}path="/3/gallery/image/#{img_id}.json"uri=URI("https://api.imgur.com"+path)request,data=Net::HTTP::Get.new(path
2022/8/4更新支持加入水印水印必须包含透明图像,并且水印图像大小要等于原图像的大小pythonconvert_image_to_video.py-f30-mwatermark.pngim_dirout.mkv2022/6/21更新让命令行参数更加易用新的命令行使用方法pythonconvert_image_to_video.py-f30im_dirout.mkvFFMPEG命令行转换一组JPG图像到视频时,是将这组图像视为MJPG流。我需要转换一组PNG图像到视频,FFMPEG就不认了。pyav内置了ffmpeg库,不需要系统带有ffmpeg工具因此我使用ffmpeg的python包装p
我正在尝试为我的iOS应用程序设置cocoapods但是当我执行命令时:sudogemupdate--system我收到错误消息:当前已安装最新版本。中止。当我进入cocoapods的下一步时:sudogeminstallcocoapods我在MacOS10.8.5上遇到错误:ERROR:Errorinstallingcocoapods:cocoapods-trunkrequiresRubyversion>=2.0.0.我在MacOS10.9.4上尝试了同样的操作,但出现错误:ERROR:Couldnotfindavalidgem'cocoapods'(>=0),hereiswhy:U
这太简单了,太荒谬了,我在任何地方都找不到关于它的任何信息,包括API文档和Rails源代码:我有一个:belongs_to关联,我开始理解当您没有关联时您在Controller中调用的正常模型方法与您有关联时调用的方法略有不同。例如,我的关联在创建Controller操作时运行良好:@user=current_user@building=Building.new(params[:building])respond_todo|format|if@user.buildings.create(params[:building])#etcetera但我找不到关于更新如何工作的文档:@user
有这样的事吗?我想在Ruby程序中使用它。 最佳答案 试试这个http://csl.sublevel3.org/jp2a/此外,Imagemagick可能还有一些东西 关于ruby-是否有将图像文件转换为ASCII艺术的命令行程序或库?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/6510445/
我正在使用Dragonfly在Rails3.1应用程序上处理图像。我正在努力通过url将图像分配给模型。我有一个很好的表格:{:multipart=>true}do|f|%>RemovePicture?Dragonfly的文档指出:Dragonfly提供了一个直接从url分配的访问器:@album.cover_image_url='http://some.url/file.jpg'但是当我在控制台中尝试时:=>#ruby-1.9.2-p290>picture.image_url="http://i.imgur.com/QQiMz.jpg"=>"http://i.imgur.com/QQ
我对图像处理完全陌生。我对JPEG内部是什么以及它是如何工作一无所知。我想知道,是否可以在某处找到执行以下简单操作的ruby代码:打开jpeg文件。遍历每个像素并将其颜色设置为fx绿色。将结果写入另一个文件。我对如何使用ruby-vips库实现这一点特别感兴趣https://github.com/ender672/ruby-vips我的目标-学习如何使用ruby-vips执行基本的图像处理操作(Gamma校正、亮度、色调……)任何指向比“helloworld”更复杂的工作示例的链接——比如ruby-vips的github页面上的链接,我们将不胜感激!如果有ruby-