UIImage from bytes held in NSString

前端 未结 3 1019
悲&欢浪女
悲&欢浪女 2021-02-04 21:23

I am trying to create a UIImage from a byte array that is actually held within a NSString.

Can someone please tell me how I can do that?

Here is what I was think

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-04 21:41

    I bet your image data has some null characters in there (0x00) and as you know that is the terminator for the string, so when you ask for the C string, you probably get way-way too little data.

    Try something like - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding; to generate your NSData.

    If that doesn't work you need to evaluate whether the setting the data into an NSString (with embedded null chars) isn't causing a loss of data too.

    Like one of the other respondents, perhaps base-64 encoding your data would be a good idea (if using a string to transport the img data is a requirement)

    Good luck.

提交回复
热议问题