你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

剪切的原始UIView为图片

2022/5/13 23:30:43

//方法说明:根据提供的位置和范围,将屏幕图像生成为UIImage并放回  

//参数1 aView : 待剪切的原始UIView  

//参数2 rect  : 剪切范围  

-(UIImage *)cropImageFrom:(UIView *)aView inRect:(CGRect)rect  

{  

    CGSize cropImageSize = rect.size;  

    UIGraphicsBeginImageContext(cropImageSize);  

    CGContextRef resizedContext = UIGraphicsGetCurrentContext();  

    CGContextTranslateCTM(resizedContext, -(rect.origin.x), -(rect.origin.y));  

    [aView.layer renderInContext:resizedContext];      

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();  

    UIGraphicsEndImageContext();   

    return image;  

}