3

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!

Gusev Andrey
  • 446
  • 5
  • 23

1 Answers1

4

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];
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
  • What "bytes" is? Size of ret-file? What do you mean "check return value,in case read was short!"? And will be data freed with NSData dealloc, or I must free it myself? – Gusev Andrey Mar 12 '12 at 08:30
  • 1
    bytes is size of your file in bytes. NSData dealloc will work. NSData will take ownership of data. – Parag Bafna Mar 12 '12 at 09:13