剪切的原始UIView为图片

来源:互联网 发布:华中科技大学网络收费 编辑:程序博客网 时间:2024/05/16 12:45

//方法说明:根据提供的位置和范围,将屏幕图像生成为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;  

}