How do I convert a UIImage to NSData or CFDataRef?

This worked for me, for a PNG image. For other image types, I assume you just have to find the corresponding UIImage...Representation method.

UIImage *image = [UIImage imageNamed:@"imageName.png"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];

If you need a CFDataRef for a UIImage, it's just one more line.

CFDataRef imgDataRef = (CFDataRef)imageData;

you can use this

NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

and simply cast imageData to CFDataRef

CFDataRef = (CFDataRef) imageData;

CFDataRef cfdata = CFDataCreate(NULL, [imageData bytes], [imageData length]);

For a working example click here.

Thank you.