iOS UIView 截屏

h文件:

@interface UIView (Screenshot)

//目前最完善

- (UIImage*) screenshotAtFrame:(CGRect)r;

@end

 

 

 

m文件:

//目前最完善

- (UIImage*) screenshotAtFrame:(CGRect)r

{

    float scale = [UIScreen mainScreen].scale*2.0;

    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, scale);

    [self.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    if (CGRectEqualToRect(self.bounds, r)){

        return theImage;

    }else{

        CGImageRef imageRef = theImage.CGImage;

        r.origin.x = r.origin.x * scale;

        r.origin.y = r.origin.y * scale;

        r.size.height = r.size.height * scale;

        r.size.width = r.size.width * scale;

        CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, r);

        UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect];

        return  sendImage;

    }

}

posted @ 2013-07-26 00:45  漫步雨桥  阅读(344)  评论(0编辑  收藏  举报