iOS 截取指定(UIView,web)区域并获取UIImage

释放双眼,带上耳机,听听看~!

获取截图


-(UIImage *)getImageFromView:(UIView *)orgView{

    CGSize s = orgView.bounds.size;

    UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);

    [orgView.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;

}

截取web指定区域


- (UIImage*)captureView:(UIView *)theView frame:(CGRect)frame
{
    UIGraphicsBeginImageContext(theView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIImage *img;
    if([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0)
    {
        for(UIView *subview in theView.subviews)
        {
            [subview drawViewHierarchyInRect:subview.bounds afterScreenUpdates:YES];
        }
        img = UIGraphicsGetImageFromCurrentImageContext();
    }
    else
    {
        CGContextSaveGState(context);
        [theView.layer renderInContext:context];
        img = UIGraphicsGetImageFromCurrentImageContext();
    }
    UIGraphicsEndImageContext();
    CGImageRef ref = CGImageCreateWithImageInRect(img.CGImage, frame);
    UIImage *CGImg = [UIImage imageWithCGImage:ref];
    CGImageRelease(ref);
    return CGImg;
}

人已赞赏
iOS文章

iOS让视图UIView 单独显示某一侧的边框线

2019-9-18 22:13:18

iOS文章

iOS 多用块枚举,少用for循环

2019-9-19 14:16:30

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索