1

I am some NSStrings, and I am just joining them and making a single NSString, then I am converting that single NSString to NSData and sending via bluetooth to other iphone

but now I have to send image with above data,

how can I achieve such concept ?

but I want to send single NSData (UIImage+NSString), how can I ????

Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154

2 Answers2

2

a tutorial on how to program Bluetooth data transfer on the iPhone is here: http://www.devx.com/wireless/Article/43502/1954

The essential part you're looking for is here:

-(IBAction) btnSend:(id) sender
{
    //---convert an NSString object to NSData---
    NSData* data;
    NSString *str = [NSString stringWithString:txtMessage.text];
    data = [str dataUsingEncoding: NSASCIIStringEncoding];        
    [self mySendDataToPeers:data];        
}

- (void) mySendDataToPeers:(NSData *) data
{
    if (currentSession) 
        [self.currentSession sendDataToAllPeers:data 
                                   withDataMode:GKSendDataReliable 
                                          error:nil];    
}

Good luck with it!

Joetjah
  • 6,292
  • 8
  • 55
  • 90
  • 1
    I think you are new to iphone programming, I told you that I can send NSString and other data, but issue is only with my UIImage, so do you know how to transfer UIImage with NString(converting to NSData)? – Chatar Veer Suthar Apr 01 '11 at 08:29
  • @Veer Ah, I think I've read the question a tad too fast... I don't know it from my own experience, but I've found the following: http://stackoverflow.com/questions/4940992/iphone-xcode-uiimage-and-uiimageview-wont-get-with-the-program The part you might be specifically interested in, is: `NSString* path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithString: @"lePhoto.png"] ]; NSData *data = [[NSData alloc] initWithContentsOfFile:path];` – Joetjah Apr 01 '11 at 08:38
  • 1
    ,, by this way you can convert a image to nsdata, but this is also not my question, i want to join it with other, and by adding this string to other strings, you will miss bits – Chatar Veer Suthar Apr 01 '11 at 09:16
0

I would recommend sending them in separate packets since an image could be quite large (send the image itself in multiple packets). But if you really want to do it all at once, try wrapping them in an NSDictionary. Encode the dictionary into NSData, and send it off. Something like the following would work.

NSDictionary *myDict = //whatever your dict should hold here...
NSMutableData *packet = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:packet]; [archiver encodeObject:myDict forKey:@"SomeKey"]; [archiver finishEncoding];

Mike
  • 2,399
  • 1
  • 20
  • 26