免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1487 | 回复: 0
打印 上一主题 下一主题

[iOS] 远程获取iOS设备的屏幕截图 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-02 11:24 |只看该作者 |倒序浏览
一个远程获取iOS设备屏幕的例子,Client采用TCP连接iOS设备的2115端口,然后读取PNG格式的数据流。

+VSRemoteScreen.h +VSRemoteScreen.m 添加到你的iOS项目中,然后在App启动时调用startScreenServer函数。

+client.php client示例文件

RemoteScreen
  1. #import <QuartzCore/QuartzCore.h>
  2. #import <sys/socket.h>
  3. #import <netinet/in.h>
  4. #import <arpa/inet.h>
  5. #import <pthread.h>

  6. CFSocketRef _socket;

  7. void* serverThread(void* context);
  8. int setupSocket(void);
  9. void acceptCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info);
  10. void sendScreenShots(CFWriteStreamRef oStream);

  11. void sendScreenShots(CFWriteStreamRef oStream) {
  12.     CGSize screenSize = [[UIScreen mainScreen] bounds].size;
  13.     UIGraphicsBeginImageContextWithOptions(screenSize, YES, 0);
  14.     CGContextRef context = UIGraphicsGetCurrentContext();
  15.     UIWindow * window = [[UIApplication sharedApplication] keyWindow];
  16.     CGContextSaveGState(context);
  17.     CGContextTranslateCTM(context, [window center].x, [window center].y);
  18.     CGContextConcatCTM(context, [window transform]);
  19.     CGContextTranslateCTM(context, -[window bounds].size.width*[[window layer] anchorPoint].x, -[window bounds].size.height*[[window layer] anchorPoint].y);
  20.     [[window layer] renderInContext:context];
  21.     CGContextRestoreGState(context);
  22.      
  23.     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  24.      
  25.     NSData* imageData = UIImagePNGRepresentation(image);
  26.      
  27.     NSUInteger offset = 0;
  28.     NSUInteger buff_size = 1024;
  29.     while(imageData.length>offset){
  30.         NSUInteger buff_len = imageData.length - offset > buff_size ? buff_size : imageData.length - offset;
  31.         CFWriteStreamWrite(oStream, imageData.bytes+offset, buff_len);
  32.         offset = offset + buff_len;
  33.     }
  34.      
  35.     UIGraphicsEndImageContext();
  36. }

  37. void acceptCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) {
  38.     if (kCFSocketAcceptCallBack == type) {
  39.         CFSocketNativeHandle nativeSocketHandle = *(CFSocketNativeHandle *)data;
  40.         uint8_t name[SOCK_MAXADDRLEN];
  41.         socklen_t nameLen = sizeof(name);
  42.         if (0 != getpeername(nativeSocketHandle, (struct sockaddr *)name, &nameLen)) {
  43.             close(nativeSocketHandle);
  44.         }
  45.          
  46.         //NSLog(@"%s connected.", inet_ntoa( ((struct sockaddr_in *)name)->sin_addr ));
  47.         CFWriteStreamRef oStream;
  48.         CFStreamCreatePairWithSocket(kCFAllocatorDefault, nativeSocketHandle, NULL, &oStream);
  49.         if (oStream) {
  50.             CFWriteStreamOpen(oStream);
  51.             sendScreenShots(oStream);
  52.             CFWriteStreamClose(oStream);
  53.             close(nativeSocketHandle);
  54.             
  55.         } else {
  56.             close(nativeSocketHandle);
  57.         }
  58.     }
  59. }

  60. int setupSocket() {
  61.     _socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketAcceptCallBack, acceptCallBack, NULL);
  62.     if (NULL == _socket) {
  63.         return 0;
  64.     }
  65.      
  66.     int optVal = 1;
  67.     setsockopt(CFSocketGetNative(_socket), SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, sizeof(optVal));
  68.      
  69.     struct sockaddr_in addr4;
  70.     memset(&addr4, 0, sizeof(addr4));
  71.     addr4.sin_len = (__uint8_t)sizeof(addr4);
  72.     addr4.sin_family = AF_INET;
  73.     addr4.sin_port = htons(2115);
  74.     addr4.sin_addr.s_addr = htonl(INADDR_ANY);
  75.     CFDataRef address = CFDataCreate(kCFAllocatorDefault, (UInt8 *)&addr4, sizeof(addr4));
  76.      
  77.     if (kCFSocketSuccess != CFSocketSetAddress(_socket, address)) {
  78.         if (_socket) CFRelease(_socket);
  79.         _socket = NULL;
  80.         return 0;
  81.     }
  82.      
  83.     CFRunLoopRef cfRunLoop = CFRunLoopGetCurrent();
  84.     CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, _socket, 0);
  85.     CFRunLoopAddSource(cfRunLoop, source, kCFRunLoopCommonModes);
  86.     CFRelease(source);
  87.      
  88.     return 1;
  89. }

  90. void* serverThread(void* context) {
  91.     @autoreleasepool {
  92.         int res = setupSocket();
  93.         if (!res) {
  94.             return 0;
  95.         }
  96.         CFRunLoopRun();
  97.         return (void*)1;
  98.     }
  99. }

  100. int startScreenServer() {
  101.     pthread_t tid;
  102.     return pthread_create(&tid, NULL, serverThread, NULL);
  103. }
复制代码
Client
  1. <?php
  2. Header("Content-type: image/png");

  3. function receiveScreenShots(){
  4.     //$srv_ip = 'localhost';
  5.     $srv_ip = '192.168.36.36';
  6.     $srv_port = 2115;
  7.     $fp = '';
  8.     $resp_str = '';
  9.     $errno = 0;
  10.     $errstr = '';
  11.     $timeout = 10;

  12.     $fp = fsockopen($srv_ip,$srv_port,$errno,$errstr,$timeout);
  13.     while(!feof($fp)){
  14.         $resp_str .= fgets($fp,512);
  15.     }
  16.     fclose($fp);

  17.     return $resp_str;
  18. }


  19. echo receiveScreenShots();
  20. ?>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP