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

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

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

代码:

#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);
}

 

支付宝打赏 微信打赏
分享到 :
VBScript替换文本
上一篇 2020-08-05
画一条虚线
2020-08-05 下一篇