我使用IOS聯繫人框架檢索iPhone用戶的聯繫人的的imageData發送。如果imageData是可用的,我檢索它並需要編碼它成一個字符串。這是因爲我正在使用Multipeer連接框架,我想在瀏覽的對等列表中顯示對等人顯示名稱旁邊的圖像。我正在發送編碼的imageData到MCNearbyServiceAdvertiser類的初始化後的discoveryInfo字典參數。IOS聯繫人框架,如何編碼和解碼的imageData轉換成String,並通過infoDiscovery字典,MPC廣告
要做到這一點,我嘗試使用UTF8和的Base64編碼的的imageData,但都失敗了。
這是我試過的UTF8編碼過程的代碼:
// Retrieve user image and initials from global userContact variable
func retrieveUserImageAndInitials() -> Dictionary<String,String> {
let userFirstNameInitial = "\(userContact.firstName[userContact.firstName.startIndex])"
let userLastNameInitial = "\(userContact.lastName[userContact.lastName.startIndex])"
if userContact.imageData != nil {
print("Image data found")
if let dataString = NSString(data: userContact.imageData!, encoding: NSUTF8StringEncoding) as String! {
print("Image data encoded successfully")
return ["firstNameInitial":userFirstNameInitial, "lastNameInitial":userLastNameInitial,"imageData":dataString]
}
else {
print("Image data encoded with failures")
return ["firstNameInitial":userFirstNameInitial, "lastNameInitial":userLastNameInitial]
}
}
else {
print("Image data not found")
return ["firstNameInitial":userFirstNameInitial, "lastNameInitial":userLastNameInitial]
}
}
這是使用UTF8解碼代碼:
func browser(browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
foundPeers.append(peerID)
let userInitials = info!["firstNameInitial"]! + info!["lastNameInitial"]!
if let dataString = info!["imageData"] {
print("Data string found")
if let imageData = dataString.dataUsingEncoding(NSUTF8StringEncoding) {
print("Image data fetched succesfully!")
imagesOfFoundPeers.append(UIImage(data: imageData)!)
}
else {
print("Image data not fetched")
imagesOfFoundPeers.append(imageFromText(userInitials,
font: UIFont(name: "Pacifico", size: 24.0)!, maxWidth: 50.0, color: UIColor.mainColor()))
}
}
else {
print("Data string not found")
print("Image with initials used instead")
imagesOfFoundPeers.append(imageFromText(userInitials,
font: UIFont(name: "Pacifico", size: 24.0)!, maxWidth: 50.0, color: UIColor.mainColor()))
}
delegate?.foundPeer()
}
在使用UTF8編碼,我從編碼過程獲得的字符串打印爲零,所以代碼甚至從來沒有發送通過廣告客戶。
這是我試過的的Base64編碼過程中的代碼:
// Retrieve user image and initials from global userContact variable
func retrieveUserImageAndInitials() -> Dictionary<String,String> {
let userFirstNameInitial = "\(userContact.firstName[userContact.firstName.startIndex])"
let userLastNameInitial = "\(userContact.lastName[userContact.lastName.startIndex])"
if userContact.imageData != nil {
print("Image data found")
let dataString = userContact.imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
print("Image data encoded successfully")
return ["firstNameInitial":userFirstNameInitial, "lastNameInitial":userLastNameInitial,"imageData":dataString]
}
else {
print("Image data not found")
return ["firstNameInitial":userFirstNameInitial, "lastNameInitial":userLastNameInitial]
}
}
這是使用的Base64 爲解碼代碼:
func browser(browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?)
{
foundPeers.append(peerID)
let userInitials = info!["firstNameInitial"]! + info!["lastNameInitial"]!
if let dataString = info!["imageData"] {
print("Data string found")
let imageData = NSData(base64EncodedString: dataString, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
let userImage = UIImage(data: imageData!)
print("Image data fetched succesfully!")
imagesOfFoundPeers.append(userImage!)
}
else {
print("Data string not found")
print("Image with initials used instead")
imagesOfFoundPeers.append(imageFromText(userInitials,
font: UIFont(name: "Pacifico", size: 24.0)!, maxWidth: 50.0, color: UIColor.mainColor()))
}
delegate?.foundPeer()
}
在使用的Base64編碼,代碼確實將數據編碼成字符串,但應用程序終止,因爲字典di包含由編碼過程產生的字符串的scoveryInfo被認爲是MCNearbyServiceAdvertiser的無效參數。
它拋出以下消息:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid discoveryInfo passed to MCNearbyServiceAdvertiser'
這是MCNearbyServiceAdvertiser的初始化和所有MPCManagement對象:
override init()
{
super.init()
userContact = retrieveUserContact()
peer = MCPeerID(displayName: userContact.fullName)
dataSession = MCSession(peer: peer)
dataSession.delegate = self
deviceBrowser = MCNearbyServiceBrowser(peer: peer, serviceType: "dummy-mpc")
deviceBrowser.delegate = self
deviceAdvertiser = MCNearbyServiceAdvertiser(peer: peer, discoveryInfo: retrieveUserImageAndInitials(), serviceType: "dummy-mpc")
deviceAdvertiser.delegate = self
}
難道我做錯了什麼,還是什麼其他選項我有編碼和發送imageData?
在此先感謝!
嘗試base64EncodedStringWithOptions(NSDataBase64EncodingOptions()) –
不要在發現信息中發送該類數據。建立連接,然後將數據發送到您的同位 – ChrisH
@ChrisH我知道,從base64編碼生成的字符串對於discoveryInfo字典來說太長了,但我希望能夠事先顯示圖像。這有點像應用程序的魔力:P我可以嘗試一下,正如Jasper Bryant-Greene所說的那樣,用Bonjour創建一個定製的發現課程,但是我在這個主題上有點綠色。 –