send UIImage as bytes? - iphone

I have a UIImageView which contains a image taken with the camera of the phone.
Next thing that i want to do is send this image to a server. I already have the socket connections set up using the CFstreams.
But i really can't get this to work. I have a UIImage object, which i want to send over somehow with my TCP connection...
I tried to send a NSData like so, but it gives me an error:
Code:
// Getting image from OpenCV and converting it back to UIImage
NSData *data = UIImagePNGRepresentation([UIImage imageWithCVMat:tpl]);
uint32_t length = (uint32_t)htonl([data length]);
// Send the image? (Doesn't work)
[outputStream write:[data bytes] maxLength:length];
error: Semantic Issue: Cannot initialize a parameter of type 'const
uint8_t *' (aka 'const unsigned char *') with an rvalue of type 'const
void *'
So anyone any idea how to send an image from a UIImage(View) as a byte array through the outputStream writer??

In this case you can safely cast:
[outputStream write:(const uint8_t *)[data bytes] maxLength:length];
since you know where the (void*) data comes from.

Related

Converting the Cipher Buffer to nsdata and back to cipherBuffer

My problem is CipherBuffer which is uint8_t i am not able to convert that to NSData and get back the same value.the thing is i need to send the encrypted data to server.
Even i get NSData from serverside and i need to convert that to (unit8_t *). Is their a way to do that i am using below method to decrypt
- (void)decryptWithPrivateKey:(uint8_t *)cipherBuffer plainBuffer:(uint8_t *)decryptedBuffer
To convert uint8_t to NSData
NSData *data = [NSData dataWithBytes:(const void *)encrypted length:bufferSize];
And to convert NSData to uint8_t you can try below method of NSData
- (const void *)bytes;

CFDataRef to NSData

I am having trouble converting a CFDataRef into NSData whilst using ARC. I am using the __bridge_transfer OR __bridge cast but it is not working. Could anyone suggest me some other way of casting this two types.
I get the following error
Automatic Reference Counting Issue: Incompatible types casting 'CFDataRef *' (aka 'const struct __CFData **') to 'NSData *' with a __bridge cast
NSData *my_nsdata = (__bridge_transfer NSData*)my_cfdata; // -1 on the my_cfdata
or
NSData *my_nsdata = (__bridge NSData*)my_cfdata; // no adjustment of retain counts.
From my blog post here:
http://amattn.com/2011/12/07/arc_best_practices.html

Sending an image data over a socket, but it receives of some different format when it reaches server

I am create a NSData file from the image and sending over server using socket.
CFWriteStreamWrite(myWriteStream, (const UInt8 *)data, [data length]);
But the server receives it in different format. and unable to open file.
What's the type of 'data' in your code snippet?
If it was a (NSData *) object, you should write like:
CFWriteStreamWrite(myWriteStream, (const UInt8 *)[data bytes], [data length]);

How can i pass NSData object over Wifi network?

How can i pass NSData object over Wifi network? can any one provide me the code to send and receive the NSData over Wifi.or any sample code/app reference .
Assuming that you know how to send data in general, here is the code:
uint8_t *bytes = (uint8_t)[myData bytes];
size_t length = [myData length];
sendBytesWithLength(bytes, length);
On the receiver side you regenerate your NSData object like this:
uint8_t *bytes = ; // Get the bytes from somewhere...
size_t length = ; // And the length
NSData *data = [[NSData alloc] initWithBytes:(const void *)bytes length:length];
Have you tried looking at the Bonjour references in the first place to set up the connection? That should lead you on to the other options for network communication.

Converting floats to NSData and back in Objective-C

In an iphone application, I'm looking to convert a float to NSData for it to be sent over bluetooth and then converted back again when it's received. I have the bluetooth part working fine, but when I use this to convert to NSData:
NSData *data = [[NSData alloc]init];
float z = 9.8574; // Get the float value, 9.8574 is just an example
[data getBytes:&z length:sizeof(float)];
I can not convert it back to a float. I've tried a couple of methods but I'm wondering if this is the correct way to encode the float to NSData??
Thanks
Here is how to encode and decode a float with NSData:
encoding:
NSMutableData * data = [NSMutableData dataWithCapacity:0];
float z = ...;
[data appendBytes:&z length:sizeof(float)];
decoding:
NSData * data = ...; // loaded from bluetooth
float z;
[data getBytes:&z length:sizeof(float)];
A couple of things to note here:
1. You have to use NSMutableData if you are going to add things to the data object after creating it. The other option is to simply load the data all in one shot:
NSData * data = [NSData dataWithBytes:&z length:sizeof(float)];
2. the getBytes:length: method is for retrieving bytes from an NSData object, not for copying bytes into it.