通过点对点同时发送字符串和 NSData?

Posted

技术标签:

【中文标题】通过点对点同时发送字符串和 NSData?【英文标题】:Send String and NSData at the same time over peer to peer? 【发布时间】:2016-09-26 17:21:15 【问题描述】:

我正在将类型为 (String) 的文本字段和类型为 (NSData) 的图像添加到将其发送给对等方的字典中,另一方面,它正在将字典解码为带有键的字符串值对。

字典中的图像数据不再是 NSData 而是一个字符串。 如何同时发送字符串和数据并将其作为字符串和数据检索?

import MultipeerConnectivity
import UIKit


class ViewController: UIViewController, MCSessionDelegate, MCBrowserViewControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate 

var browser : MCBrowserViewController!
var assistant : MCAdvertiserAssistant!
var session : MCSession!
var peerID: MCPeerID!

var firstNameVar = ""
var lastNameVar = ""
var imageDataVar: NSData!

let imagePicker  = UIImagePickerController()
var tapGestureID = Int()

@IBOutlet weak var firstLame: UITextField!
@IBOutlet weak var lastName: UITextField!
@IBOutlet weak var firstNameLabel: UILabel!
@IBOutlet weak var lastNameLabel: UILabel!
@IBOutlet weak var contactImage: UIImageView!

@IBAction func showBrowser(sender: UIButton) 
    self.presentViewController(self.browser, animated: true, completion: nil)


@IBAction func getImage(sender: AnyObject) 
    chooseImageContact()


@IBAction func sendChat(sender: AnyObject) 
    sendInfo()


override func viewDidLoad() 
    super.viewDidLoad()
    self.peerID = MCPeerID(displayName: UIDevice.currentDevice().name)

    //self.session = MCSession(peer: peerID, securityIdentity: nil, encryptionPreference: .Required)
    self.session = MCSession(peer: self.peerID)
    self.session.delegate = self

    // create the browser viewcontroller with a unique service name
    self.browser = MCBrowserViewController(serviceType: "LCOC-Chat", session: self.session)
    self.browser.delegate = self

    // tell the assistant to start advertising our fabulous chat
    self.assistant = MCAdvertiserAssistant(serviceType:"LCOC-Chat", discoveryInfo:nil, session:self.session)
    self.assistant.start()


func sendInfo() 
    if self.session.connectedPeers.count > 0 
        firstNameVar = firstLame.text!
        lastNameVar = lastName.text!
        let myDictionary = ["itemA" : "\(firstNameVar)", "itemB" : "\(lastNameVar)", "itemC" : "\(imageDataVar)"]
        do 
        let data =  NSKeyedArchiver.archivedDataWithRootObject(myDictionary)
        try self.session.sendData(data, toPeers: self.session.connectedPeers, withMode: MCSessionSendDataMode.Unreliable)
         catch let error as NSError 
                let ac = UIAlertController(title: "Send error", message: error.localizedDescription, preferredStyle: .Alert)
                    ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
                    presentViewController(ac, animated: true, completion: nil)
            
    
    self.firstLame.text = ""
    self.lastName.text = ""



// Called when a peer sends an NSData to us
func session(session: MCSession, didReceiveData data: NSData, fromPeer peerID: MCPeerID) 

     // This needs to run on the main queue
    dispatch_async(dispatch_get_main_queue()) 
    let myDictionary = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! NSDictionary
        self.firstNameLabel.text = myDictionary.valueForKey("itemA") as? String
        self.lastNameLabel.text = myDictionary.valueForKey("itemB") as? String

//            self.firstNameVar = myDictionary.valueForKey("itemA") as? String
//            self.lastNameVar = myDictionary.valueForKey("itemB") as? String
//            let image = myDictionary.valueForKey("itemC") as? String
//            let imageData = NSKeyedUnarchiver.unarchiveObjectWithData(image) as? NSData

    


func browserViewControllerDidFinish(browserViewController: MCBrowserViewController) 
    dismissViewControllerAnimated(true, completion: nil)

func browserViewControllerWasCancelled(browserViewController: MCBrowserViewController) 
    dismissViewControllerAnimated(true, completion: nil)

func browserViewController(browserViewController: MCBrowserViewController, shouldPresentNearbyPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) -> Bool 
    return true


func session(session: MCSession, peer peerID: MCPeerID, didChangeState state: MCSessionState) 
    switch state 
    case MCSessionState.Connected:
        print("Connected: \(peerID.displayName)")
    case MCSessionState.Connecting:
        print("Connecting: \(peerID.displayName)")
    case MCSessionState.NotConnected:
        print("Not Connected: \(peerID.displayName)")
    


func session(session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, withProgress progress: NSProgress) 

func session(session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, atURL localURL: NSURL, withError error: NSError?) 

func session(session: MCSession, didReceiveStream stream: NSInputStream, withName streamName: String, fromPeer peerID: MCPeerID) 


func textFieldShouldReturn(textField: UITextField) -> Bool 
    textField.resignFirstResponder()
    return true



// Picking the image
func chooseImageContact()
    let imagePicker        = UIImagePickerController()
    imagePicker.delegate   = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    self.presentViewController(imagePicker, animated: true, completion: nil)


//Scalling the image
func scaleContactImageWith(image:UIImage, newSize:CGSize)->UIImage
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
    image.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
    let newContactImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newContactImage


func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) 
    //This gets the Contact image inside the imagePickerController
        let pickedImage:UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
        let smallPicture        = scaleContactImageWith(pickedImage, newSize: CGSizeMake(100, 100))
        var sizeOfImage:CGRect  = contactImage.frame
        sizeOfImage.size    = smallPicture.size
        contactImage.frame  = sizeOfImage
        picker.dismissViewControllerAnimated(true, completion: nil)
        contactImage.image = smallPicture

        imageDataVar = UIImagePNGRepresentation(smallPicture)!
    


【问题讨论】:

【参考方案1】:

您当前正在使用数据的description 方法将NSData 转换为字符串。永远不要那样做。

更好的解决方案是简单地将数据按原样放入字典中,因为您正在归档字典并发送数据。无需将图像数据转换成字符串。

然后在接收端,您将收到的数据解压缩,并且字典中已经包含图像的 PNG 数据。

【讨论】:

谢谢 _let myDictionary = ["itemA" : "(firstNameVar)", "itemB" : "(lastNameVar)", "itemC" : imageDataVar] 很高兴为您提供帮助。请不要忘记接受可以解决您问题的答案。

以上是关于通过点对点同时发送字符串和 NSData?的主要内容,如果未能解决你的问题,请参考以下文章

java 通过TCPUDP 协议实现多人聊天,点对点,文件传送-----分服务器端和客户端

学习ActiveMQ:点对点(队列)模式消息演示

网络中的点对点到底是啥意思,啥原理啊?

SpringBoot---Web Socket(广播式、点对点)

Android Socket UDP 点对点,或者广播通讯,包含发送端和接收端

Android Socket UDP 点对点,或者广播通讯,包含发送端和接收端