Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

ios - Placing *both* string and image on pasteboard

I am trying to put both a string and an image onto the pasteboard. If I use

let str : String
let img : UIImage

// ...

UIPasteboard.general.items = [ [ String ( kUTTypeText  ) : str ],
                               [ String ( kUTTypeImage ) : img ] ]

the image pastes fine but not the string. The string is in this weird encoded form.

I've tried different types as well as converting the string to NSString or UTF8 but it either crashes with value for pasteboard type 'public.text' is not a valid property list type or the string remains in some coded form.

EDIT

If I paste it into Notes this is what I see.

Pasted text and image in Notes

and if I tap on the text it expands into

Pasted text in Notes

while if I add

print ( "Copied ( str )" )

I get

Copied 960 939 379 918 958 884 971 672 962 127 852 754 715 ...

in the console.

Also, if I just do

UIPasteboard.general.string = str

it works as expected albeit then I only copy a string.

question from:https://stackoverflow.com/questions/65856771/placing-both-string-and-image-on-pasteboard

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should use kUTTypeUTF8PlainText as type key.

BR Dirk


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...