Send JPG Image via Socket on iPhone

– (void) stream: (NSStream *) stream handleEvent: (NSStreamEvent) eventCode
{
@try {
switch(eventCode)
{
case NSStreamEventOpenCompleted: //open completed
{
NSData *data = UIImageJPEGRepresentation(drawImage.image, 90);
// Convert from native to host endianness
byteData = (Byte *)[data bytes];
len = [data length];
break;
}
case NSStreamEventHasSpaceAvailable: //has space available
{
if(stream == oStream)
{
uint8_t *length = (uint8_t *)(len);
// Don’t forget to check the return value of ‘write’
[oStream write:length maxLength:4];
NSInteger intReturn = [oStream write:byteData maxLength:len];
len -= intReturn;
byteData += intReturn;
}
break;
}
case NSStreamEventEndEncountered: //end encountered
{
[activityIndicator stopAnimating];
[oStream close]; //close output stream
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Successful”
message:nil
delegate:self
cancelButtonTitle:@”OK”
otherButtonTitles:nil];
[alert show];
[alert release];
break;
}
case NSStreamEventErrorOccurred: //error occurred
{
[activityIndicator stopAnimating];
[oStream close];
break;
}
}
}
@catch (NSException * e) {
[activityIndicator stopAnimating];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[e reason]
message:nil
delegate:nil
cancelButtonTitle:@”OK”
otherButtonTitles:nil];
[alert show];
[alert release];
}
}

This entry was posted in iPhone. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s