无法从iOS应用

时间:2018-06-05 18:24:19

标签: ios chromecast

我正在使用iOS 11.4和Swift 4构建iOS应用程序,假设在iOS屏幕上显示本地图像库。当我从图库中选择单个图像时,我想通过Chromecast将该图像发送到电视屏幕。 我可以连接到我的Chromecast设备,但是当我尝试发送图片时,它只会在电视屏幕上显示投射图标。以下是将图像传输到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!)
}
})

每次调用loadMedia时都会刷新电视屏幕,但显示的唯一内容是强制转换图标。有人可以查看这段代码,看看是否有什么错误导致图像无法发送到电视屏幕?

1 个答案:

答案 0 :(得分:0)

我能够解决这个问题。为了能够将图像发送到chromecast,然后发送到电视屏幕,我不得不将Web服务器嵌入到我的iOS应用程序中。然后,我必须托管要在Web服务器上电视上显示的每个图像,并将Web服务器URL发送到chromecast,以便chromecast能够向我的嵌入式服务器发出http请求,并将图像拉到chromecast。