3

我试图截取可扩展的 uitableview 的屏幕截图,我试图截取 uitableview 的屏幕截图但我无法做到,然后我将 tableview 框架大小更改为 tableview 内容大小。截屏后,我将帧大小更改为旧帧大小。现在的问题是我在表格视图中有可扩展部分。我可以在我的屏幕截图中截取未展开的表格视图单元格的屏幕截图。我也需要截取未展开的表格视图单元格内容的屏幕截图。

4

1 回答 1

1

通过使用此类别文件,您可以截取屏幕上可见的屏幕截图....

  @interface UIImage (MyImage)


    + (UIImage*)imageFromView:(UIView*)view;
    + (UIImage*)imageFromView:(UIView*)view scaledToSize:(CGSize)newSize;
    + (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
    + (void)beginImageContextWithSize:(CGSize)size;
    + (UIImage *)croppedImage:(UIImage *)myImage :(CGRect)bounds;
    @end

#import "UIImage+MyImage.h"

@implementation UIImage (MyImage)

+ (void)beginImageContextWithSize:(CGSize)size
{
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        if ([[UIScreen mainScreen] scale] == 2.0) {
            UIGraphicsBeginImageContextWithOptions(size, YES, 2.0);
        } else {
            UIGraphicsBeginImageContext(size);
        }
    } else {
        UIGraphicsBeginImageContext(size);
    }
}

+ (void)endImageContext
{
    UIGraphicsEndImageContext();
}

+ (UIImage*)imageFromView:(UIView*)view
{
    [self beginImageContextWithSize:[view bounds].size];
    BOOL hidden = [view isHidden];
    [view setHidden:NO];
    [[view layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    [self endImageContext];
    [view setHidden:hidden];
    return image;
}

+ (UIImage*)imageFromView:(UIView*)view scaledToSize:(CGSize)newSize
{
    UIImage *image = [self imageFromView:view];
    if ([view bounds].size.width != newSize.width ||
        [view bounds].size.height != newSize.height) {
        image = [self imageWithImage:image scaledToSize:newSize];
    }
    return image;
}

+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
    [self beginImageContextWithSize:newSize];
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    [self endImageContext];
    return newImage;
}


+ (UIImage *)croppedImage:(UIImage *)myImage :(CGRect)bounds {
    CGImageRef imageRef = CGImageCreateWithImageInRect(myImage.CGImage, bounds);
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    CGSize asd =  croppedImage.size;
    return croppedImage;
}

@end

方法调用将是....

 yourImageView.image= [UIImage imageFromView:(UIView*)yourTableViewInstance ]

尝试这个....

于 2012-09-10T08:42:08.830 回答