通过点对点同时发送 String 和 NSData?
Send String and NSData at the same time over peer to peer?
我正在将类型为 (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)!
}
}
您当前正在使用数据的 description
方法将 NSData
转换为字符串。永远不要那样做。
一个更好的解决方案是简单地将数据按原样放入字典中,因为您正在归档字典并发送数据。无需将图片数据转成字符串
然后在接收端,您取消存档接收到的数据,字典中已有该图像的 PNG 数据。
我正在将类型为 (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)!
}
}
您当前正在使用数据的 description
方法将 NSData
转换为字符串。永远不要那样做。
一个更好的解决方案是简单地将数据按原样放入字典中,因为您正在归档字典并发送数据。无需将图片数据转成字符串
然后在接收端,您取消存档接收到的数据,字典中已有该图像的 PNG 数据。