美文网首页IOS 开发相关ios开发小技巧
iOS View转换成清晰的image图片

iOS View转换成清晰的image图片

作者: 任梦RM | 来源:发表于2018-03-01 14:40 被阅读825次
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        [self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        UIView *BGView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 110+150+150)];
        BGView.backgroundColor = [UIColor whiteColor];
        
        UIImageView *topImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 110)];
        topImageView.image = [UIImage imageNamed:@"topImage.png"];
        [BGView addSubview:topImageView];
        
        UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 110+20, 320-40, 150-40)];
        textLabel.numberOfLines = 0;
        textLabel.text = @"下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了,关键就是第三个参数";
    //    textLabel.backgroundColor = [UIColor whiteColor];
        textLabel.textColor = [UIColor blueColor];
        [BGView addSubview:textLabel];
        
        UIImageView *bottomImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 110+150, 320, 150)];
        bottomImageView.image = [UIImage imageNamed:@"bottomImage.png"];
        [BGView addSubview:bottomImageView];
        
    
        UIImageView *testImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 50, 320, 110+150+150)];
        testImageView.image = [self makeImageWithView:BGView withSize:BGView.frame.size];;
        [self.view addSubview:testImageView];
        
    }
    
    
    #pragma mark 生成image
    - (UIImage *)makeImageWithView:(UIView *)view withSize:(CGSize)size
    {
        
        // 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了,关键就是第三个参数 [UIScreen mainScreen].scale。
        UIGraphicsBeginImageContextWithOptions(size, YES, [UIScreen mainScreen].scale);
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
        
    }
    

    相关文章

      网友评论

        本文标题:iOS View转换成清晰的image图片

        本文链接:https://www.haomeiwen.com/subject/eibgxftx.html