美文网首页
iOS如何截屏或view生成image之后支持背景透明

iOS如何截屏或view生成image之后支持背景透明

作者: wpina | 来源:发表于2020-05-20 23:15 被阅读0次

    - (UIImage *)makeImageWithView:(UIView *)orgView withSize:(CGSize)size

    {

        CGFloat scale = [UIScreen mainScreen].scale;

        UIGraphicsBeginImageContextWithOptions(orgView.bounds.size, NO, scale);

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSaveGState(context);

        [orgView.layer renderInContext:context];

        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        //把像素rect 转化为点rect(如无转化则按原图像素取部分图片)

        CGFloat x= orgView.frame.origin.x*scale,y=orgView.frame.origin.y*scale,w=orgView.frame.size.width*scale,h=orgView.frame.size.height*scale;

        CGRect dianRect = CGRectMake(x, y, w, h);

        //截取部分图片并生成新图片

        CGImageRef sourceImageRef = [image CGImage];

        CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, dianRect);

        UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:scale orientation:UIImageOrientationUp];

        return newImage;

    }

    相关文章

      网友评论

          本文标题:iOS如何截屏或view生成image之后支持背景透明

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