convert UIImage to NSData
Try one of the following, depending on your image format:
UIImageJPEGRepresentation
Returns the data for the specified image in JPEG format.
NSData * UIImageJPEGRepresentation ( UIImage *image, CGFloat compressionQuality);
UIImagePNGRepresentation
Returns the data for the specified image in PNG format
NSData * UIImagePNGRepresentation ( UIImage *image);
EDIT:
if you want to access the raw bytes that make up the UIImage, you could use this approach:
CGDataProviderRef provider = CGImageGetDataProvider(image.CGImage);NSData* data = (id)CFBridgingRelease(CGDataProviderCopyData(provider));const uint8_t* bytes = [data bytes];
This will give you the low-level representation of the image RGB pixels.(Omit the CFBridgingRelease
bit if you are not using ARC).
NSData *imageData = UIImagePNGRepresentation(myImage.image);
If you have an image inside a UIImageView , e.g. "myImageView", you can do the following:
Convert your image using UIImageJPEGRepresentation() or UIImagePNGRepresentation() like this:
NSData *data = UIImagePNGRepresentation(myImageView.image);//orNSData *data = UIImageJPEGRepresentation(myImageView.image, 0.8);//The float param (0.8 in this example) is the compression quality //expressed as a value from 0.0 to 1.0, where 1.0 represents //the least compression (or best quality).
You can also put this code inside a GCD block and execute in another thread, showing an UIActivityIndicatorView during the process ...
//*code to show a loading view here*dispatch_queue_t myQueue = dispatch_queue_create("com.my.queue", DISPATCH_QUEUE_SERIAL);dispatch_async(myQueue, ^{ NSData *data = UIImagePNGRepresentation(myImageView.image); //some code.... dispatch_async( dispatch_get_main_queue(), ^{ //*code to hide the loading view here* });});