I am making image receiver with iPhone.
The server send image file with byte.
and, i tried to make this bytes to image.
But, i can not make image. already, several days past...
i really need your help...
this is source code that i use.
Byte recvBuffer[500];
memset(recvBuffer, '\0', sizeof(recvBuffer));
[iStream read:recvBuffer maxLength:sizeof(recvBuffer)-1];
NSUInteger len = sizeof(recvBuffer);
NSData *webdata = [NSData dataWithBytes:recvBuffer length:len];
imageview.image = [UIImage imageWithData:webdata];
if you give a comment, i will really appriciate! thank you for reading!
badgerr's answer is substantially the one I would use, but since the question is tagged Objective-C
the code should really be in Objective-C - omething like the following. Note that over a TCP/IP connection, -read:maxLength: may return before the requested number of bytes has been read.
uint32_t size; // Let's make sure size is an explicit width.
NSInteger totalBytesRead = 0;
NSInteger bytesRead = [istream read: &size maxLength: sizeof size];
while (bytesRead > 0 && totalBytesRead + bytesRead < sizeof size)
{
totalBytes+= bytesRead;
bytesRead = [istream read: &size + totalBytesRead maxLength: (sizeof size) - totalBytesRead];
}
if (bytesRead >= 0)
{
totalBytesRead += bytesRead;
}
else
{
// read failure, report error and bail
}
if (totalBytesRead < sizeof size)
{
// connection closed before we got the whole size, report and bail
}
size = ntohl(size); // assume wire protocol uses network byte ordering
NSMutableData* buffer = [[NSMutableData alloc] initWithLength: size];
totalBytesRead = 0;
bytesRead = [istream read: [buffer mutableBytes] maxLength: size];
while (bytesRead > 0 && totalBytesRead + bytesRead < size)
{
totalBytes+= bytesRead;
bytesRead = [istream read: (char*)[buffer mutableBytes] + totalBytesRead maxLength: size - totalBytesRead];
}
if (bytesRead >= 0)
{
totalBytesRead += bytesRead;
}
else
{
// read failure, report error and bail (not forgetting to release buffer)
}
if (totalBytesRead < size)
{
// connection closed before we got the whole image, report and bail (not forgetting to release buffer)
}
else
{
[buffer setLength: size];
}
imageView.image = [UIImage imageWithData: buffer];
[buffer release];
I typed the above straight in to SO, so it is not tested or even compiled.
TCP/IP is a protocol which is OS independent, so the fact that you're using XP on the server should not affect anything, aside from potentially using the windows API for socket operations.
Claus Broch is correct in saying that 499 bytes looks a bit small for an image. What I suggest you do instead is first find the total size of the image (in bytes) on the server, then send that value at the beginning of the TCP stream, followed by the image data. On the receiving end, your code should first read this size value, allocate some memory of that size, then read that many bytes into it. You haven't posted your server code, so I can't help with that, but the client could look a little something like:
unsigned int size = 0; //assumes the size is sent as an int
Byte* recvBuffer;
[iStream read:&size maxLength:sizeof(unsigned int)];
recvBuffer = new Byte[size];
memset(recvBuffer, 0, size);
[iStream read:recvBuffer maxLength:size];
NSData *webdata = [NSData dataWithBytes:recvBuffer length:size];
imageview.image = [UIImage imageWithData:webdata];
delete [] recvBuffer;
Edit My inner C++ made me use new and delete. While you can use this in Obj-C using the .mm file suffix, this question is tagged Objective-C. You should use JeremyP's answer, as it also includes error checking and a more reliable way of reading the complete image.