码迷,mamicode.com
首页 > 移动开发 > 详细

远程获取iOS设备的屏幕截图

时间:2015-01-12 22:38:32      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

一个远程获取iOS设备屏幕的例子,Client采用TCP连接iOS设备的2115端口,然后读取PNG格式的数据流。

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

+client.php client示例文件

[1].[代码] RemoteScreen 跳至 [1] [2]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#import <QuartzCore/QuartzCore.h>
#import <sys/socket.h>
#import <netinet/in.h>
#import <arpa/inet.h>
#import <pthread.h>
 
CFSocketRef _socket;
 
void* serverThread(void* context);
int setupSocket(void);
void acceptCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info);
void sendScreenShots(CFWriteStreamRef oStream);
 
void sendScreenShots(CFWriteStreamRef oStream) {
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    UIGraphicsBeginImageContextWithOptions(screenSize, YES, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, [window center].x, [window center].y);
    CGContextConcatCTM(context, [window transform]);
    CGContextTranslateCTM(context, -[window bounds].size.width*[[window layer] anchorPoint].x, -[window bounds].size.height*[[window layer] anchorPoint].y);
    [[window layer] renderInContext:context];
    CGContextRestoreGState(context);
     
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
     
    NSData* imageData = UIImagePNGRepresentation(image);
     
    NSUInteger offset = 0;
    NSUInteger buff_size = 1024;
    while(imageData.length>offset){
        NSUInteger buff_len = imageData.length - offset > buff_size ? buff_size : imageData.length - offset;
        CFWriteStreamWrite(oStream, imageData.bytes+offset, buff_len);
        offset = offset + buff_len;
    }
     
    UIGraphicsEndImageContext();
}
 
void acceptCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) {
    if (kCFSocketAcceptCallBack == type) {
        CFSocketNativeHandle nativeSocketHandle = *(CFSocketNativeHandle *)data;
        uint8_t name[SOCK_MAXADDRLEN];
        socklen_t nameLen = sizeof(name);
        if (0 != getpeername(nativeSocketHandle, (struct sockaddr *)name, &nameLen)) {
            close(nativeSocketHandle);
        }
         
        //NSLog(@"%s connected.", inet_ntoa( ((struct sockaddr_in *)name)->sin_addr ));
        CFWriteStreamRef oStream;
        CFStreamCreatePairWithSocket(kCFAllocatorDefault, nativeSocketHandle, NULL, &oStream);
        if (oStream) {
            CFWriteStreamOpen(oStream);
            sendScreenShots(oStream);
            CFWriteStreamClose(oStream);
            close(nativeSocketHandle);
             
        } else {
            close(nativeSocketHandle);
        }
    }
}
 
int setupSocket() {
    _socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketAcceptCallBack, acceptCallBack, NULL);
    if (NULL == _socket) {
        return 0;
    }
     
    int optVal = 1;
    setsockopt(CFSocketGetNative(_socket), SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, sizeof(optVal));
     
    struct sockaddr_in addr4;
    memset(&addr4, 0, sizeof(addr4));
    addr4.sin_len = (__uint8_t)sizeof(addr4);
    addr4.sin_family = AF_INET;
    addr4.sin_port = htons(2115);
    addr4.sin_addr.s_addr = htonl(INADDR_ANY);
    CFDataRef address = CFDataCreate(kCFAllocatorDefault, (UInt8 *)&addr4, sizeof(addr4));
     
    if (kCFSocketSuccess != CFSocketSetAddress(_socket, address)) {
        if (_socket) CFRelease(_socket);
        _socket = NULL;
        return 0;
    }
     
    CFRunLoopRef cfRunLoop = CFRunLoopGetCurrent();
    CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, _socket, 0);
    CFRunLoopAddSource(cfRunLoop, source, kCFRunLoopCommonModes);
    CFRelease(source);
     
    return 1;
}
 
void* serverThread(void* context) {
    @autoreleasepool {
        int res = setupSocket();
        if (!res) {
            return 0;
        }
        CFRunLoopRun();
        return (void*)1;
    }
}
 
int startScreenServer() {
    pthread_t tid;
    return pthread_create(&tid, NULL, serverThread, NULL);
}

[2].[代码] Client 跳至 [1] [2]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
Header("Content-type: image/png");
 
function receiveScreenShots(){
    //$srv_ip = ‘localhost‘;
    $srv_ip = ‘192.168.36.36‘;
    $srv_port = 2115;
    $fp = ‘‘;
    $resp_str = ‘‘;
    $errno = 0;
    $errstr = ‘‘;
    $timeout = 10;
 
    $fp = fsockopen($srv_ip,$srv_port,$errno,$errstr,$timeout);
    while(!feof($fp)){
        $resp_str .= fgets($fp,512);
    }
    fclose($fp);
 
    return $resp_str;
}
 
 
echo receiveScreenShots();
?>

远程获取iOS设备的屏幕截图

标签:

原文地址:http://blog.csdn.net/u014311077/article/details/42649525

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!