今天有个直播截屏的需求,首先用了下面这个方法:
UIGraphicsBeginImageContextWithOptions(self.previewView.frame.size, NO, [[UIScreen mainScreen]scale]);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.previewView.layer renderInContext:context];
UIImage *targetImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
结果啥也截不到
一顿百度发现上面这种方法只能截取UIKit里的view,换了iOS7新加入的方法:
UIGraphicsBeginImageContextWithOptions(self.previewView.frame.size, NO, [UIScreen mainScreen].scale);
[self.previewView drawViewHierarchyInRect:self.previewView.frame afterScreenUpdates:YES];
UIImage *targetImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
妥了