Skip to content

Instantly share code, notes, and snippets.

@fireblue
Last active November 1, 2016 02:28
Show Gist options
  • Select an option

  • Save fireblue/5027217 to your computer and use it in GitHub Desktop.

Select an option

Save fireblue/5027217 to your computer and use it in GitHub Desktop.

Revisions

  1. fireblue revised this gist Mar 20, 2013. No changes.
  2. fireblue revised this gist Mar 20, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion UIWebView+Screenshot.m
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    -(UIImage *)screenShot
    {
    CGRect originalFrame = self.frame;
  3. fireblue revised this gist Mar 20, 2013. 1 changed file with 40 additions and 39 deletions.
    79 changes: 40 additions & 39 deletions UIWebView+Screenshot.m
    Original file line number Diff line number Diff line change
    @@ -1,45 +1,46 @@

    -(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 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;
    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;
    }
  4. fireblue revised this gist Feb 25, 2013. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions UIWebView+Screenshot.m
    Original file line number Diff line number Diff line change
    @@ -38,8 +38,8 @@ -(UIImage *)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;
    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;
    }
  5. fireblue renamed this gist Feb 25, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. fireblue created this gist Feb 25, 2013.
    45 changes: 45 additions & 0 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    -(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;
    }