-(UIImage *)screenShot { CGRect originalFrame = self.frame; CGPoint originalOffset = self.scrollView.contentOffset; CGSize entireSize = [self sizeThatFits:CGSizeZero]; [self setFrame: CGRectMake(0, 0, entireSize.width, entireSize.height)]; CGRect rect = [self positionOfElementWithId:@"post1"]; //如果没有找到这个元素,就取整个页面 if (rect.origin.y != 0) { entireSize.height = rect.origin.y; } entireSize.height += 40; //第三个参数为0就是取系统默认的scale,支持retina。网上带if的只是兼容3.X以下的系统。 UIGraphicsBeginImageContextWithOptions(entireSize, NO, 0); CGContextRef context = UIGraphicsGetCurrentContext(); [self.layer renderInContext:context]; //多出来的部分画个白色背景色以放置我们尾部的文字 CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0); CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0); CGContextFillRect(context, CGRectMake(0, entireSize.height-30, entireSize.width, 30)); CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0); CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0); NSString *contentString = @“这里是你需要加上的文字 UIFont *font = [UIFont systemFontOfSize:12]; [contentString drawInRect:CGRectMake(12, entireSize.height-27, entireSize.width, 25) withFont:font lineBreakMode:UILineBreakModeWordWrap]; UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [self setFrame:originalFrame]; self.scrollView.contentOffset = originalOffset; return screenshot; } - (CGRect)positionOfElementWithId:(NSString *)elementID { NSString *js = @"function f(){ var r = document.getElementById('%@').getBoundingClientRect(); return '{{'+r.left+','+r.top+'},{'+r.width+','+r.height+'}}'; } f();"; NSString *result = [self stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:js, elementID]]; CGRect rect = CGRectFromString(result); return rect; }