3

Been battling this one for a while. Basically, I am converting an image into NSData so I can send it to a server. The code I have used before, but for some reason I am getting an ARC error on this. The error lands on the line I declare the imageData variable.

NOTE: myImage is handed to the method.

- (void)uploadImage:(NSImage *)myImage {

     NSData *imageData = UIImageJPEGRepresentation(myImage, 1.0);

     // Do something...

}

I get an error and two warnings

Error: Implicit conversion of 'int' to 'NSData *' is disallowed with ARC
Warning: Implicit declaration of function 'UIImageJPEGRepresentation' is invalid in C99
Warning: Incompatible integer to pointer conversion intializing 'NSData * __strong' with an expression of type 'int'

Any ideas?

Jeffrey Hunter
  • 1,103
  • 2
  • 12
  • 19

1 Answers1

7

You might need to include the relevant header:

#import <UIKit/UIKit.h>

Prior to C99, if you call a function that the compiler hasn't seen a declaration for, it will compile the call as if the function was declared as int UIImageJPEGRepresentation(). C99 doesn't allow that, but it seems that the compiler is still applying the old interpretation (or the compiler is in pre-C99 mode; I'm not sure what the default is), hence the ARC error.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • Not that it makes a huge difference, but you'd normally use `#import` rather than `#include`. But yeah, UIKit is probably what's missing. – Siobhán Jul 29 '12 at 23:52
  • It's a Desktop App not iPhone. Does that matter? – Jeffrey Hunter Jul 29 '12 at 23:53
  • @JeffreyHunter: Probably not, but I can't say for sure. If the function is declared in a different header in the Mac OS X SDKs, you may have to import something else. – Marcelo Cantos Jul 29 '12 at 23:55
  • @JeffreyHunter: Oh, yeah, that does matter. UIKit is iOS-only, so basically anything starting with "UI" won't work on OS X. You have to do a bit more work with NSImage than with UIImage, but [this](http://cocoadev.com/wiki/NSImageToJPEG) looks like it has what you need. – Siobhán Jul 30 '12 at 00:00
  • On second thought, that's not terribly readable and doesn't get you all the way to an NSData instance. Have a look here instead: [http://stackoverflow.com/a/3213017/1411506](http://stackoverflow.com/a/3213017/1411506). – Siobhán Jul 30 '12 at 00:08