'Trouble sending a local image to Chromecast from iOS app
I am building an iOS app with iOS 11.4 and Swift 4 which is suppose to display a gallery of local images on the iOS screen. When I select a single image from the gallery, I want to send that image to TV screen through Chromecast. I can connect to my Chromecast device, but when I try to send the image, it shows only cast icon on the TV screen. Here is the code for transmitting the image to Chromecast:
PHImageManager.default().requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFit, options: options, resultHandler: { image, _ in
// If successful, show the image view and display the image.
guard let image = image else { return }
// Now that we have the image, show it.
imageview.isHidden = false
// Here the image is correctly displayed on the screen
imageview.image = image
// Checking if cast session started
if self.sessionManager.hasConnectedSession() == true {
// Creating image name to save it as URL
var imageName = "Image_"
// Give image unique name
imageName = imageName + "\(self.imageNameInt)"
self.imageNameInt += 1
// Saving image to local URL so it will be compatible for use
// in GCKMediaInformation
let imageData = UIImageJPEGRepresentation(imageview.image!, 0.8)
let docDir = try! FileManager.default.url(for: .documentDirectory,
in: .userDomainMask, appropriateFor: nil, create: true)
let imageURL = docDir.appendingPathComponent(imageName)
try! imageData?.write(to: imageURL)
let metadata = GCKMediaMetadata(metadataType: .photo)
let imageAsset = imageURL.absoluteString
self.mediaInfo = GCKMediaInformation(
contentID: imageAsset,
streamType: .buffered,
contentType: "image/jpg",
metadata: metadata,
streamDuration: 0,
mediaTracks: nil,
textTrackStyle: nil,
customData: nil)
self.sessionManager.currentCastSession?.remoteMediaClient?.loadMedia(self.mediaInfo!)
}
})
Every time loadMedia is called TV screen is refreshed, but the only thing that is displayed is cast icon. Can somebody look into this code and see if there is something wrong which can cause image not to be sent to the TV screen?
Solution 1:[1]
I was able to solve this problem. In order to be able to send an image to chromecast and consequently to TV screen, I had to embed web server into my iOS app. I then had to host every image I wanted to display on TV on web server and to send web server url to the chromecast so the chromecast would be able to make http request to my embedded server and pull the image to the chromecast.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | dstojano |