簡體   English   中英

使用Multipeer連接發送圖像

[英]Issue sending image using Multipeer Connectivity

我有一個使用Multipeer Connectivity在設備之間發送文件的應用程序。 在接收設備上, didFinishReceivingResourceWithNamelocalURL被調用,但是localURL的文件似乎為空。 代碼如下:

func sendPhoto() {
    guard let imageData = UIImagePNGRepresentation(photo) else { return }
    imageData.writeToURL(photoURL, atomically: true)
    // photoURL: "/var/mobile/Containers/Data/Application/0A2C6681-FA7C-4821-8C21-E9C3768E84EC/Documents/temp/A9081EE5-5D43-499D-8DC0-D36FEC041FE0.png"

    session.sendResourceAtURL(photoURL, withName: "image.png", toPeer: otherPeer) { 
        error in
            print(error)
    }
}

func session(session: MCSession, didFinishReceivingResourceWithName resourceName: String,
    fromPeer peerID: MCPeerID, atURL localURL: NSURL, withError error: NSError?) {
        print(error) // error is nil
        do {
            try NSFileManager.defaultManager().moveItemAtURL(localURL, toURL: profilePhotoURL)
            // profilePhotoURL: file:///var/mobile/Containers/Data/Application/FE5CF814-5BD4-40A9-9444-C8776F3153DC/Documents/temp/5535AACE-2C7D-4337-BBCF-8F62BC51239C.png
        } catch { print(error) }

        let image = UIImage(contentsOfFile: profilePhotoURL.absoluteString) // image is nil
}

我也嘗試過將NSData放置在localURL ,但最終結果為nil

photo是從用戶的照片庫中選擇的UIImage 對可能出什么問題有任何想法嗎?

更多信息:

我檢查了接收設備上的文件系統,並且圖像在那里。 似乎是某種原因導致UIImage構造函數因通過MPC接收到的任何圖像而失敗。

示例項目:

我在GitHub上編寫了一個小型復制器應用程序 您將需要兩個iOS設備進行測試。

我嘗試了這個,對我來說,我要做的就是立即從本地URL轉換為NSData,如下所示:

func session(session: MCSession, didFinishReceivingResourceWithName resourceName: String,
fromPeer peerID: MCPeerID, atURL localURL: NSURL, withError error: NSError?) {
    var image: UIImage? = nil
    if let localData = NSData(contentsOfURL: localURL) {
        image = UIImage(data: localData)
    }
}

localURL打印為什么? 我的直接懷疑是您需要一個以file://為前綴的URL字符串,以便從磁盤讀取...這也將解釋為什么來自URL的數據也為零。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM