How do I convert a UIImage
to NSData
or CFDataRef
? I need to pass a CFDataRef
to ABPersonSetImageData
.
CFDataRef cfdata = CFDataCreate(NULL, [imageData bytes], [imageData length]);
For a working example click here.
Thank you.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With