0

I am getting an error while using the following method for reading image.

[NSString stringWithContentsOfFile:imagepath encoding:NSUTF8StringEncoding error:&error

The error is The file “image.png” couldn’t be opened using text encoding Unicode (UTF-8).

Pratik
  • 103
  • 1
  • 9

1 Answers1

0

An image is always saved as NSData not as string

NSData *data = [NSData dataWithContentsOfFile:imagepath options:0 error:&error];

For sending the data to a server create a string for example base64 encoded but this depends on what format the server expects

if (data != nil) {
   NSString *string = [data base64EncodedStringWithOptions:0];
}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • I'm not sure, what he wants.. could also be a base64 encoded string or whatever ;-) – d4Rk Dec 26 '18 at 11:29
  • @d4Rk A base64 encoded string doesn't cause this error and `.png` points to an image file. – vadian Dec 26 '18 at 11:30
  • Ok, what about it is saved as data, and he wants do send it to the backend as base64? I would not do it like that, but totally possible, isn't it? – d4Rk Dec 26 '18 at 11:31
  • @d4Rk To *read* the file you have to use the `NSData` API anyway (or `UIImage` to create an image). – vadian Dec 26 '18 at 11:33
  • Whatever, just wanted to make my point, that it is totally not clear what he wants to do. (despite loading a image first) – d4Rk Dec 26 '18 at 11:34
  • @d4Rk I want to send an image to a server with string format – Pratik Dec 26 '18 at 11:36