1. 程式人生 > >view 截圖方法

view 截圖方法

1.


- (UIImage*)photoImageSnapshot {

    UIGraphicsBeginImageContextWithOptions(self.bounds.size,

                                           NO, [UIScreenmainScreen].scale

);

    [self drawViewHierarchyInRect:self.boundsafterScreenUpdates:YES];

    UIImage* newImage =UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;

}

呼叫: 

 UIImage *image = [self.photoBackView

photoImageSnapshot];

2.

-(UIImage *)takeSnapshot

{

    @autoreleasepool {

        UIGraphicsBeginImageContext(self.bounds.size);

        CGContextRef context =UIGraphicsGetCurrentContext();

        [self.layer

renderInContext:context];

        UIImage *resultingImage =UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        return resultingImage;

    }

}

呼叫: 

 UIImage *image = [self.photoBackView takeSnapshot];



推薦第一種:第二種有點失真。

UIGraphicsBeginImageContext 與 UIGraphicsBeginImageContextWithOptions 功能相同,相當於UIGraphicsBeginImageContextWithOptions的opaque引數為NO,scale因子為1.0。