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.photoBackViewphotoImageSnapshot];

2.

-(UIImage *)takeSnapshot

{

    @autoreleasepool {

        UIGraphicsBeginImageContext(self.bounds.size);

        CGContextRef context =UIGraphicsGetCurrentContext();

        [self.layerrenderInContext:context];

        UIImage *resultingImage =UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        return resultingImage;

    }

}

调用: 

 UIImage *image = [self.photoBackView takeSnapshot];



推荐第一种:第二种有点失真。

UIGraphicsBeginImageContext 与 UIGraphicsBeginImageContextWithOptions 功能相同,相当于UIGraphicsBeginImageContextWithOptions的opaque参数为NO,scale因子为1.0。






猜你喜欢

转载自blog.csdn.net/haixing_zfj/article/details/75303827
今日推荐