如何使用Chromecast在电视上发送iOS照片?

如何使用Chromecast在电视上发送iOS照片?,ios,objective-c,uiimage,chromecast,television,Ios,Objective C,Uiimage,Chromecast,Television,那些日子我一直在玩Chromecast SDK。我目前尝试的是使用Chromecast在电视上发送UIImage(例如iPhone拍摄的照片) 我可以使用URL加载“外部”图像,但我不知道如何发送本地存储的图像 那么,有没有一种方法可以使用base64编码发送它,或者设置一个流,甚至镜像屏幕?我有点迷路了,如果有人能给我一个提示或一些示例代码,那就太好了 您可以在应用程序中托管一个小型web服务器,然后将该服务器的URL提供给Chromecast receiver应用程序,以从您的设备加载照片。

那些日子我一直在玩Chromecast SDK。我目前尝试的是使用Chromecast在电视上发送UIImage(例如iPhone拍摄的照片)

我可以使用URL加载“外部”图像,但我不知道如何发送本地存储的图像


那么,有没有一种方法可以使用base64编码发送它,或者设置一个流,甚至镜像屏幕?我有点迷路了,如果有人能给我一个提示或一些示例代码,那就太好了

您可以在应用程序中托管一个小型web服务器,然后将该服务器的URL提供给Chromecast receiver应用程序,以从您的设备加载照片。Cast协议频道不是为处理大型二进制传输而设计的。

您可以在应用程序中托管一个小型web服务器,然后将该服务器的URL提供给Chromecast receiver应用程序,以从设备加载照片。Cast协议通道不是为处理大型二进制传输而设计的。

是!您可以使用CocoaHTTPServer,它是一种小型、轻量级、可嵌入的HTTP服务器,适用于Mac OS X或iOS应用程序

#import "iPhoneHTTPServerAppDelegate.h"
#import "iPhoneHTTPServerViewController.h"
#import "HTTPServer.h"
#import "DDLog.h"
#import "DDTTYLogger.h"

// Log levels: off, error, warn, info, verbose
static const int ddLogLevel = LOG_LEVEL_VERBOSE;


@implementation iPhoneHTTPServerAppDelegate

@synthesize window;
@synthesize viewController;

- (void)startServer
{
    // Start the server (and check for problems)

    NSError *error;
    if([httpServer start:&error])
    {
        DDLogInfo(@"Started HTTP Server on port %hu", [httpServer listeningPort]);
    }
    else
    {
        DDLogError(@"Error starting HTTP Server: %@", error);
    }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Configure our logging framework.
    // To keep things simple and fast, we're just going to log to the Xcode console.
    [DDLog addLogger:[DDTTYLogger sharedInstance]];

    // Create server using our custom MyHTTPServer class
    httpServer = [[HTTPServer alloc] init];

    // Tell the server to broadcast its presence via Bonjour.
    // This allows browsers such as Safari to automatically discover our service.
    [httpServer setType:@"_http._tcp."];

    // Normally there's no need to run our server on any specific port.
    // Technologies like Bonjour allow clients to dynamically discover the server's port at runtime.
    // However, for easy testing you may want force a certain port so you can just hit the refresh button.
    // [httpServer setPort:12345];

    // Serve files from our embedded Web folder
    NSString *webPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Web"];
    DDLogInfo(@"Setting document root: %@", webPath);

    [httpServer setDocumentRoot:webPath];

    [self startServer];

    // Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    return YES;
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [self startServer];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // There is no public(allowed in AppStore) method for iOS to run continiously in the background for our purposes (serving HTTP).
    // So, we stop the server when the app is paused (if a users exits from the app or locks a device) and
    // restart the server when the app is resumed (based on this document: http://developer.apple.com/library/ios/#technotes/tn2277/_index.html )

    [httpServer stop];
}



@end

对!!您可以使用CocoaHTTPServer,它是一种小型、轻量级、可嵌入的HTTP服务器,适用于Mac OS X或iOS应用程序

#import "iPhoneHTTPServerAppDelegate.h"
#import "iPhoneHTTPServerViewController.h"
#import "HTTPServer.h"
#import "DDLog.h"
#import "DDTTYLogger.h"

// Log levels: off, error, warn, info, verbose
static const int ddLogLevel = LOG_LEVEL_VERBOSE;


@implementation iPhoneHTTPServerAppDelegate

@synthesize window;
@synthesize viewController;

- (void)startServer
{
    // Start the server (and check for problems)

    NSError *error;
    if([httpServer start:&error])
    {
        DDLogInfo(@"Started HTTP Server on port %hu", [httpServer listeningPort]);
    }
    else
    {
        DDLogError(@"Error starting HTTP Server: %@", error);
    }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Configure our logging framework.
    // To keep things simple and fast, we're just going to log to the Xcode console.
    [DDLog addLogger:[DDTTYLogger sharedInstance]];

    // Create server using our custom MyHTTPServer class
    httpServer = [[HTTPServer alloc] init];

    // Tell the server to broadcast its presence via Bonjour.
    // This allows browsers such as Safari to automatically discover our service.
    [httpServer setType:@"_http._tcp."];

    // Normally there's no need to run our server on any specific port.
    // Technologies like Bonjour allow clients to dynamically discover the server's port at runtime.
    // However, for easy testing you may want force a certain port so you can just hit the refresh button.
    // [httpServer setPort:12345];

    // Serve files from our embedded Web folder
    NSString *webPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Web"];
    DDLogInfo(@"Setting document root: %@", webPath);

    [httpServer setDocumentRoot:webPath];

    [self startServer];

    // Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    return YES;
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [self startServer];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // There is no public(allowed in AppStore) method for iOS to run continiously in the background for our purposes (serving HTTP).
    // So, we stop the server when the app is paused (if a users exits from the app or locks a device) and
    // restart the server when the app is resumed (based on this document: http://developer.apple.com/library/ios/#technotes/tn2277/_index.html )

    [httpServer stop];
}



@end

基于Leon和Alok提供的响应,即使用Cocoa HTTP服务器通过HTTP从iOS设备提供图像服务,您可以在at上找到一个示例,其中有详细说明

另外,不要忘记,要为您的ChromeCast提供服务,您需要启用CORS

简而言之,一旦将Cocoa HTTP服务器添加到项目中,您就可以

  • 子类HTTPDataResponse,如下所示,以启用CORS
  • CamCaptureDataResponse.h

    CamCaptureDataResponse.m #导入“CamCaptureDataResponse.h”

  • 通过将HTTPConnection子类化,在您自己的请求处理程序中使用这个新的DataResponse类
  • CamCaptureConnection.h

    #import "HTTPConnection.h"
    
    @interface CamCaptureConnection : HTTPConnection
    
    @end
    
    #import "CamCaptureConnection.h"
    #import "CamCaptureHTTPServer.h"
    #import "CamCaptureDataResponse.h"
    
    @implementation CamCaptureConnection
    
    -(NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:  (NSString *)path {
        NSArray* pathComponents = [path componentsSeparatedByString:@"/"];
    
        if ([pathComponents count] < 2) {
            return [[CamCaptureDataResponse alloc] initWithData:[@"ERROR" dataUsingEncoding:NSUTF8StringEncoding]];
        }
    
        NSString *command = [pathComponents objectAtIndex:1];
    
        if ([command isEqualToString:@"PING"]) {
            return [[CamCaptureDataResponse alloc] initWithData:[@"PONG"     dataUsingEncoding:NSUTF8StringEncoding]];
        }
    
        if ([command isEqualToString:@"PIC"]) {
            // Change the following line with whichever image you want to serve to your ChromeCast!
            NSData *imageData = UIImageJPEGRepresentation([CamCaptureHttpServer instance].captureImage, 0.3); 
    
            if (imageData) {
                return [[CamCaptureDataResponse alloc] initWithData:imageData];
            } else {
                return [[CamCaptureDataResponse alloc] initWithData:[@"NO_IMAGE" dataUsingEncoding:NSUTF8StringEncoding]];
            }
        }
    
        return [[CamCaptureDataResponse alloc] initWithData:[@"ERROR_UNKNOWN_COMMAND" dataUsingEncoding:NSUTF8StringEncoding]];
    }
    
    @end
    
    CamCaptureConnection.m

    #import "HTTPConnection.h"
    
    @interface CamCaptureConnection : HTTPConnection
    
    @end
    
    #import "CamCaptureConnection.h"
    #import "CamCaptureHTTPServer.h"
    #import "CamCaptureDataResponse.h"
    
    @implementation CamCaptureConnection
    
    -(NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:  (NSString *)path {
        NSArray* pathComponents = [path componentsSeparatedByString:@"/"];
    
        if ([pathComponents count] < 2) {
            return [[CamCaptureDataResponse alloc] initWithData:[@"ERROR" dataUsingEncoding:NSUTF8StringEncoding]];
        }
    
        NSString *command = [pathComponents objectAtIndex:1];
    
        if ([command isEqualToString:@"PING"]) {
            return [[CamCaptureDataResponse alloc] initWithData:[@"PONG"     dataUsingEncoding:NSUTF8StringEncoding]];
        }
    
        if ([command isEqualToString:@"PIC"]) {
            // Change the following line with whichever image you want to serve to your ChromeCast!
            NSData *imageData = UIImageJPEGRepresentation([CamCaptureHttpServer instance].captureImage, 0.3); 
    
            if (imageData) {
                return [[CamCaptureDataResponse alloc] initWithData:imageData];
            } else {
                return [[CamCaptureDataResponse alloc] initWithData:[@"NO_IMAGE" dataUsingEncoding:NSUTF8StringEncoding]];
            }
        }
    
        return [[CamCaptureDataResponse alloc] initWithData:[@"ERROR_UNKNOWN_COMMAND" dataUsingEncoding:NSUTF8StringEncoding]];
    }
    
    @end
    

    基于Leon和Alok提供的响应,即使用Cocoa HTTP服务器通过HTTP从iOS设备提供图像服务,您可以在at上找到一个示例,其中有详细说明

    另外,不要忘记,要为您的ChromeCast提供服务,您需要启用CORS

    简而言之,一旦将Cocoa HTTP服务器添加到项目中,您就可以

  • 子类HTTPDataResponse,如下所示,以启用CORS
  • CamCaptureDataResponse.h

    CamCaptureDataResponse.m #导入“CamCaptureDataResponse.h”

  • 通过将HTTPConnection子类化,在您自己的请求处理程序中使用这个新的DataResponse类
  • CamCaptureConnection.h

    #import "HTTPConnection.h"
    
    @interface CamCaptureConnection : HTTPConnection
    
    @end
    
    #import "CamCaptureConnection.h"
    #import "CamCaptureHTTPServer.h"
    #import "CamCaptureDataResponse.h"
    
    @implementation CamCaptureConnection
    
    -(NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:  (NSString *)path {
        NSArray* pathComponents = [path componentsSeparatedByString:@"/"];
    
        if ([pathComponents count] < 2) {
            return [[CamCaptureDataResponse alloc] initWithData:[@"ERROR" dataUsingEncoding:NSUTF8StringEncoding]];
        }
    
        NSString *command = [pathComponents objectAtIndex:1];
    
        if ([command isEqualToString:@"PING"]) {
            return [[CamCaptureDataResponse alloc] initWithData:[@"PONG"     dataUsingEncoding:NSUTF8StringEncoding]];
        }
    
        if ([command isEqualToString:@"PIC"]) {
            // Change the following line with whichever image you want to serve to your ChromeCast!
            NSData *imageData = UIImageJPEGRepresentation([CamCaptureHttpServer instance].captureImage, 0.3); 
    
            if (imageData) {
                return [[CamCaptureDataResponse alloc] initWithData:imageData];
            } else {
                return [[CamCaptureDataResponse alloc] initWithData:[@"NO_IMAGE" dataUsingEncoding:NSUTF8StringEncoding]];
            }
        }
    
        return [[CamCaptureDataResponse alloc] initWithData:[@"ERROR_UNKNOWN_COMMAND" dataUsingEncoding:NSUTF8StringEncoding]];
    }
    
    @end
    
    CamCaptureConnection.m

    #import "HTTPConnection.h"
    
    @interface CamCaptureConnection : HTTPConnection
    
    @end
    
    #import "CamCaptureConnection.h"
    #import "CamCaptureHTTPServer.h"
    #import "CamCaptureDataResponse.h"
    
    @implementation CamCaptureConnection
    
    -(NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:  (NSString *)path {
        NSArray* pathComponents = [path componentsSeparatedByString:@"/"];
    
        if ([pathComponents count] < 2) {
            return [[CamCaptureDataResponse alloc] initWithData:[@"ERROR" dataUsingEncoding:NSUTF8StringEncoding]];
        }
    
        NSString *command = [pathComponents objectAtIndex:1];
    
        if ([command isEqualToString:@"PING"]) {
            return [[CamCaptureDataResponse alloc] initWithData:[@"PONG"     dataUsingEncoding:NSUTF8StringEncoding]];
        }
    
        if ([command isEqualToString:@"PIC"]) {
            // Change the following line with whichever image you want to serve to your ChromeCast!
            NSData *imageData = UIImageJPEGRepresentation([CamCaptureHttpServer instance].captureImage, 0.3); 
    
            if (imageData) {
                return [[CamCaptureDataResponse alloc] initWithData:imageData];
            } else {
                return [[CamCaptureDataResponse alloc] initWithData:[@"NO_IMAGE" dataUsingEncoding:NSUTF8StringEncoding]];
            }
        }
    
        return [[CamCaptureDataResponse alloc] initWithData:[@"ERROR_UNKNOWN_COMMAND" dataUsingEncoding:NSUTF8StringEncoding]];
    }
    
    @end
    

    是否可以在应用程序中托管web服务器?你能简单地解释一下如何做到这一点吗(只是为了给我指出正确的方向)?例如,看看这个项目:可以在应用程序中托管web服务器吗?你能简单地解释一下如何做到这一点吗(只是为了给我指出正确的方向)?例如,看看这个项目:虽然这两个链接可以提供OP问题的答案,但请至少在这里提供回答问题的关键代码/信息。嗨,贝诺特,我添加我的答案是为了根据我引用的项目添加一些实际代码。虽然这两个链接可能提供OP问题的答案,但请至少在此处提供回答问题的关键代码/信息。你好,Benoit,我添加我的答案是为了根据我引用的项目添加一些实际代码。