i am trying to read jpg file and convert into byte array
this is the following code this code works fine with text file but files on image files
// start code
NSString *stringFromFileAtPath = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if(stringFromFileAtPath == nil){
NSLog(@"Error reading file at path %@\n%@", path, [error localizedFailureReason]);
}
NSLog(@"Contents:%@", stringFromFileAtPath);
NSData *bytes = [stringFromFileAtPath dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"Bytes:%@", bytes);
// end of code
and this is error message i am getting
Error Message:Text encoding Unicode (UTF-8) isn’t applicable.
Let me know what is the error i am doing or code to convert jpg file to byte array
Why not just use
NSData‘sinitWithContentsOfFile:method? See here:i.e.:
The reason you can’t do what you did is that you’re trying to read the image file as if it is encoded in UTF8, which it’s highly likely there’s bytes in there that would be malformed in UTF8 – it’s not a string.