2011-04-01 52 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];