探讨在tweak中高效的截屏方法

我使用这个帖子中的截屏方法http://stackoverflow.com/questions/13971013/taking-screenshots-in-the-background-ios-improving-performance
IOSurfaceRef surface = [UIWindow createScreenIOSurface];
UIImage *surfaceImage = [UIImage alloc] _initWithIOSurface:surface imageOrientation:UIImageOrientationUp]
CFRelease(surface);
但是使用UIImageWriteToSavedPhotosAlbum保存surfaceImage 却没有保存进图库
使用NSData *data = UIImagePNGRepresentation(surfaceImage); data居然为空
但是sufaceImage却不为空 在UIImageView里面可以正常显示
这种截屏方法据上面的帖子中是 0.5-0.7 sec/screenshot. 太慢了 有没有更加高效的后台截屏方法,或者有没有更加底层的私有截屏API还有就是使用createScreenIOSurface 为啥不能保存
请大大名赐教

我在.xm文件中用以下代码可以达到全局截屏的效果
extern “C” UIImage *_UICreateScreenUIImage();

UIImage *screenImage = _UICreateScreenUIImage();
UIImageWriteToSavedPhotosAlbum(screenImage, nil, nil, nil);

参考:http://iphonedevwiki.net/index.php/UIImage#UICreateScreenUIImage

这个就是同时按下home和menu键截屏时的底层实现,我觉得效率就算很好了,如果你感兴趣就继续往底层逆向呗

大大 _UICreateScreenUIImage这个api可以在我自己的daemon里面用么 为什么我在springboard里面用可以 但在自己的daemon里面 截出来的都是黑色的?

#import <UIKit/UIKit.h>
extern "C" UIImage *_UICreateScreenUIImage();
static void Reboot(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
    UIImage *screenImage= _UICreateScreenUIImage();
    NSString *path=@"/var/tmp/screenImage.png";
    NSData *fileData = UIImagePNGRepresentation(screenImage);
    NSLog(@"saveScreen:%@",[fileData description]);
    [fileData writeToFile:path atomically:YES];
}
int main(int argc, char **argv, char **envp) {
    NSLog(@"daemon: daemond is launched!");
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, Reboot, CFSTR("com.hwj.mydaemon"), NULL, CFNotificationSuspensionBehaviorCoalesce);
    CFRunLoopRun();
    return 0;
}

因为daemon没有界面,所以截屏就是黑的

  • (UIImage *)imageFromView: (UIView *) theView
    {

    UIGraphicsBeginImageContext(theView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [theView.layer renderInContext:context];
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return theImage;
    }
    通过上面的方法可以截图并保存
    传入参数UIView 只有获取 本APP的
    我如何去截其他的APP的图片 其他APP的UIView 如何获取 求教

你需要的就是同时按下lock和home的效果吧?参考这个帖子,SpringBoard的截屏就是这么实现的

谢谢大大 我碰到的问题和aspireios 一样 在daemon中进行截图 因为我在daemon中建立了一个网络通讯 想通过发送截图指令 然后 将图片回传给pc 现在就是图片截图不到

参考这个帖子

我也是这么做的 网络通讯给daemon发送消息截图 daemon通过进程通信发消息给springboard截图 在springboard里面调用_UICreateScreenUIImage()截图 ,用_UICreateScreenUIImage()好一些可以放到指定的路径 home+lock键好像是直接保存到相册的 不方便取 而且还有声音

谢谢 这个方法是有效的 通过进程通讯 控制springboard进行截图 就可以截图成功

ok了 谢谢大大

大神,我代码试一下,发现应该不是系统的home+menu(power?)键的底层实现吧。
普通view都可以截,但是拍照界面不能截啊,只能截到上面一层拍照操作按钮的view,摄像头采集到的实景画面出不来,是一片黑。而系统的截屏是可以截拍照界面的啊!求教!!