在Nativescript中使用本机NStextattachment和NSAttribute字符串在textview中添加图像
我只是尝试使用原生NSAtrributedstring和NStextAttachment在textview中添加一个图像,从本文中获得了一些帮助 然而,我无法做到这一点。我正在使用nativescript mediafilepicker库添加照片库中的图像,然后使用其内置方法将PH图像转换为UIImage。但是textview没有使用图像进行更新。但是,我可以通过NSattributedstring添加更多字符串,而不是图像 这是我的密码在Nativescript中使用本机NStextattachment和NSAttribute字符串在textview中添加图像,nativescript,Nativescript,我只是尝试使用原生NSAtrributedstring和NStextAttachment在textview中添加一个图像,从本文中获得了一些帮助 然而,我无法做到这一点。我正在使用nativescript mediafilepicker库添加照片库中的图像,然后使用其内置方法将PH图像转换为UIImage。但是textview没有使用图像进行更新。但是,我可以通过NSattributedstring添加更多字符串,而不是图像 这是我的密码 //creating
//creating and initilizing new NSMutableAttributedString
var attributedString = NSMutableAttributedString.alloc().initWithString(textview.ios.text);
textview.ios.attributedText = attributedString;
//value is an UIImage object what the convertPHImageToUIImage method returns
var image = value;
console.log(image);
//the above log prints<UIImage: 0x2817ac4d0> size {4032, 3024} orientation 0 scale 1.000000
let oldWidth = image.size.width;
// console.log(oldWidth);
let scaleFactor = oldWidth / (textview.ios.frame.size.width - 10);
//console.log(scaleFactor);
let orientation="up";
//create NStextAttachment
let textAttachment = NSTextAttachment.alloc().init();
//adding UIImage object to NSTextAttachment
textAttachment.image = UIImage.imageWithCGImageScaleOrientation(image.CGImage ,scaleFactor , orientation)
// console.dir(textAttachment);
//creating a new NSAttributedString
let attrStringWithImage = NSAttributedString.alloc().init();
//console.dir(attrStringWithImage);
attrStringWithImage.attachment = textAttachment;
console.dir(attrStringWithImage)
//appenind the NSAttributedString to the mutable string..
attributedString.appendAttributedString(attrStringWithImage);
//console.log(attributedString.containsAttachmentsInRange(textview.ios.selectedRange));
textview.ios.attributedText = attributedString;
//textview.ios.textStorage.insertAttributedStringAtIndex(attrStringWithImage,textview.ios.selectedRange.location)
//this doesn't work either
如果您使用的是TypeScript,请安装,这将使您在希望访问本机API时的生活变得轻松
UIImage.imageWithCGImageScaleOrientation(cgImage, scale, orientation);
这将帮助您理解将Objective C转换为JavaScript/TypeScript。请注意!我是一个业余的发展。如果我问了一些愚蠢的问题,请原谅HelloSir,非常感谢。了解使用参数名称的第一个字母作为大写的概念。谢谢。我现在就实施。希望它能工作您好,先生,在实现了同样的功能之后,当medifilepicker打开时,我得到了一个奇怪的日志:不能在非主线程上使用asCopy=NO调用。无法在非主线程上使用asCopy=NO调用。无法在非主线程上使用asCopy=NO调用。无法在非主线程上使用asCopy=NO调用。无法在非主线程上使用asCopy=NO调用。连接到XPCPhotoLibraryStore您的图像的来源是什么?你能重现一下游乐场的问题吗?你好,先生,我可以一直到现在,但是文本视图仍然没有用图像更新。我也参考了这篇文章,我现在把我的代码放到了问题中。app/images/1.jpg将不是UIImage的已知路径。