【问题标题】:how to send images via bluetooth in iphone programming?如何在 iPhone 编程中通过蓝牙发送图像?
【发布时间】:2011-04-01 08:21:30
【问题描述】:

我是一些 NSString,我只是加入他们并制作一个 NSString,然后我将那个 NSString 转换为 NSData 并通过蓝牙发送到其他 iphone

但现在我必须发送带有上述数据的图像,

我怎样才能实现这样的概念?

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

【问题讨论】:

    标签: iphone iobluetooth


    【解决方案1】:

    关于如何在 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];    
    }
    

    祝你好运!

    【讨论】:

    • 我认为你是 iphone 编程新手,我告诉过你我可以发送 NSString 和其他数据,但问题只是我的 UIImage,所以你知道如何使用 NString 传输 UIImage(转换为NSData)?
    • @Veer 啊,我觉得我读这个问题有点太快了......我不知道我自己的经验,但我发现了以下内容:stackoverflow.com/questions/4940992/… The您可能特别感兴趣的部分是:NSString* path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithString: @"lePhoto.png"] ]; NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    • ,,通过这种方式你可以将图像转换为 nsdata,但这也不是我的问题,我想将它与其他字符串连接起来,并且通过将此字符串添加到其他字符串,你会错过位
    【解决方案2】:

    我建议将它们以单独的数据包发送,因为图像可能非常大(以多个数据包发送图像本身)。但如果你真的想一次完成所有操作,请尝试将它们包装在 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];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      • 2012-05-13
      • 1970-01-01
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 2016-05-07
      相关资源
      最近更新 更多