2011-04-01 101 views
1

我一些NSString的,而我只是将它们连接,使一个单一的NSString,那么我认为单一的NSString转换为NSData的,并通过蓝牙发送给其他iPhone如何通过iphone程序中的蓝牙发送图像?

但现在我必须与上述数据发送图像,

我该如何实现这样的概念?

但我想发送单个NSData(UIImage + NSString),我怎么能?

回答

2

有关如何编程的iPhone上的蓝牙数据传输的教程是在这里: http://www.devx.com/wireless/Article/43502/1954

你要找的关键部分是在这里:

-(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];  
} 

祝你好运吧!

+1

我想你是新iPhone编程,我告诉你,我可以发送的NSString和其他数据,但问题是,只与我的UIImage,让你知道如何传送的UIImage与NString(转换为NSData的)? – 2011-04-01 08:29:27

+0

@Veer啊,我想我已经读过这个问题太快了......我不知道从我自己的经验,但我已经找到以下内容:http://stackoverflow.com/questions/4940992您可能特别感兴趣的部分是:'NSString * path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:@“lePhoto.png” ]]; NSData * data = [[NSData alloc] initWithContentsOfFile:path];' – Joetjah 2011-04-01 08:38:33

+1

,,通过这种方式可以将图像转换为nsdata,但这也不是我的问题,我想与其他人一起加入,并通过添加此字符串到其他字符串,你会错过位 – 2011-04-01 09:16:51

0

因为图像可能相当大(将图像本身发送到多个数据包中),所以我建议将它们发送到单独的数据包中。但是如果你真的想一次完成所有这些,可以试试把它们包装在一个NSDictionary中。将字典编码到NSData中,并将其发送出去。 像下面这样的东西可以工作。

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];

相关问题