转换NSAttributedString后,图像不会显示

时间:2016-12-13 12:51:22

标签: ios swift nsattributedstring

我试图用NSKeyedUnarchiver转换它:

存档:

let content_file = NSKeyedArchiver.archivedData(withRootObject: postContentTextView.attributedText!)

以txt文件的形式上传到服务器:

multipartFormData.append(content_file, withName: "post[content_file]", fileName: randomAlphaNumericString(10), mimeType: "text/plain")

稍后从服务器中检索它并取消归档:

let attributedString = NSKeyedUnarchiver.unarchiveObject(with: reponse.data!) as! NSAttributedString

但是当我显示未归档的NSAttributedString时,它会显示文本,但不显示图像。

编辑:

这就是我将图像添加到UITextView的方式:

func addImage(image : UIImage) {
    let attributedString = NSMutableAttributedString(attributedString: postContentTextView.attributedText!)
    let textAttachment = NSTextAttachment()
    textAttachment.image = image

    let oldWidth = textAttachment.image!.size.width;

    let scaleFactor = oldWidth / (postContentTextView.frame.size.width - 10); //for the padding inside the textView
    textAttachment.image = UIImage(cgImage: textAttachment.image!.cgImage!, scale: scaleFactor, orientation: .up)
    let attrStringWithImage = NSAttributedString(attachment: textAttachment)
    attributedString.replaceCharacters(in: postContentTextView.selectedRange, with: attrStringWithImage)
    postContentTextView.attributedText = attributedString;
}

0 个答案:

没有答案