1

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

1 Answer 1

4

Why not just use NSData's initWithContentsOfFile: method? See here:

i.e.:

NSData *bytes = [[NSData alloc] initWithContentsOfFile:path]; NSLog(@"Bytes:%@", bytes); 

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.

Sign up to request clarification or add additional context in comments.

1 Comment

i have a same problem. but i need to convert .db filwe which size is 4.0mb. When i use ur code for small file size it works fine.But when i try to convert 4.1mb file my app is crash. Is there any aletrnative or solution for my problem? Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.