Building up on responses provided by Leon and Alok, i.e. serving images from your iOS device over HTTP using Cocoa HTTP server, you can find an example on at GitHub with detailed explanation in this blog post.
Also don't forget that to be served to your ChromeCast, you will need to enable CORS.
In short, and once you have added Cocoa HTTP Server to your project, you can
- subclass HTTPDataResponse as follows in order to enable CORS
CamCaptureDataResponse.h
#import "HTTPDataResponse.h"
@interface CamCaptureDataResponse : HTTPDataResponse
@end
CamCaptureDataResponse.m
#import "CamCaptureDataResponse.h"
@implementation CamCaptureDataResponse
-(NSDictionary*)httpHeaders {
return @{
@"Access-Control-Allow-Origin":@"*",
@"Access-Control-Allow-Methods":@"GET,PUT,POST,DELETE",
@"Access-Control-Allow-Headers":@"Content-Type"
};
}
@end
- Use this new DataResponse class in your own request handler by subclassing HTTPConnection
CamCaptureConnection.h
#import "HTTPConnection.h"
@interface CamCaptureConnection : HTTPConnection
@end
CamCaptureConnection.m
#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
Then before you start, your web server, first register your new connection class as follows
NSError *error;
httpServer = [[CamCaptureHttpServer alloc] init];
[httpServer setConnectionClass:[CamCaptureConnection class]];
[httpServer setType:@"_http._tcp."];
[httpServer setPort:1234];
[httpServer start:&error];