Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load file to NSData with C

Problem is to load image file to UIImage. Original file cashed in file system and managed by C-library (question about it).

With objective-C I can do so:

NSData *file = [NSData dataWithContentsOfFile:...];
[UIImage imageWithData:file];

But how it possible to load file data to objective-C with C?

With C I can open file this like:

ret = fopen( name, options);

But can I use the ret ptr to init NSData:

[NSData dataWithBytesNoCopy:ret length:length_of_ret];?

Thanks a lot for any help!

like image 664
Gusev Andrey Avatar asked Dec 05 '25 05:12

Gusev Andrey


1 Answers

Read bytes from file using fread.

FILE * ret = fopen( name, options);
void *data = malloc(bytes);
fread(data, 1, bytes, ret); 
fclose(ret);
 NSData *data = [NSData dataWithBytesNoCopy:data length:bytes];
like image 195
Parag Bafna Avatar answered Dec 07 '25 19:12

Parag Bafna



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!