iOS一些绘图经验

来源:互联网 发布:网页版游戏源码 编辑:程序博客网 时间:2024/06/11 18:46

需求:将相机拍出的图片+一个拉伸背景图片+另一个图片合成一张图片 + 底图一些额外的图片信息

一、正确的方案

UIGraphicsBeginImageContextWithOptions((self.cameraView?.bounds.size)!, false0);

self.cameraView?.drawHierarchy(in: (self.cameraView?.bounds)!, afterScreenUpdates: true)

let newIkkkmage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!

UIGraphicsEndImageContext()

//合成的图片

let newIkkkmage:UIImage = (UIGraphicsGetImageFromCurrentImageContext())!

 UIGraphicsBeginImageContextWithOptions(CGSize(width:cameraView.frame.size.width,height:self.view.height), false0.0)

 newIkkkmage.draw(in: CGRect(x:0 ,y:0 ,width:cameraView.frame.size.width ,height:cameraView.frame.size.height))

 let companyInfoImage:UIImage = UIImage.init(named: "appImage")!

 let cuttOffLineImage:UIImage = UIImage.init(named: "cutOffLineImage")!

 let qrCodeImage:UIImage = UIImage.init(named: "qrCodeImage")!    

 let companyInfoImageViewLeftPadding:CGFloat = 12

 let companyInfoImageViewWidth:CGFloat = 184

 let companyInfoImageViewHeight:CGFloat = 48

 let companyInfoImageViewBottom:CGFloat = 47

 let companyInfoFrame:CGRect = CGRect(x:companyInfoImageViewLeftPadding,y:KDeviceHeight - companyInfoImageViewBottom - companyInfoImageViewHeight,width:companyInfoImageViewWidth,height:companyInfoImageViewHeight)

            companyInfoImage.draw(in: companyInfoFrame)

let qrCodeImageViewRightPadding:CGFloat = 18

let qrCodeImageViewWidth:CGFloat = 60

let qrCodeImageViewHeight:CGFloat = 60

let qrCodeImageViewBottom:CGFloat = 41

let qrCodeImageViewFrame: CGRect = CGRect(x:KDeviceWidth - qrCodeImageViewRightPadding - qrCodeImageViewWidth,y:KDeviceHeight - qrCodeImageViewBottom - qrCodeImageViewHeight,width:qrCodeImageViewWidth,height:qrCodeImageViewHeight)

            qrCodeImage.draw(in: qrCodeImageViewFrame)

let cutOffImageViewRightPadding:CGFloat = 18

let cutOffImageViewWidth:CGFloat = 2

let cutOffImageViewHeight:CGFloat = 80

let cutOffImageViewBottom:CGFloat = 31

let cutOffLineImageViewFrame:CGRect =  CGRect(x:qrCodeImageViewFrame.origin.x - cutOffImageViewRightPadding - cutOffImageViewWidth ,y:KDeviceHeight - cutOffImageViewHeight - cutOffImageViewBottom,width:cutOffImageViewWidth,height:cutOffImageViewHeight)

            cuttOffLineImage.draw(in: cutOffLineImageViewFrame)         

 let newImage:UIImage = (UIGraphicsGetImageFromCurrentImageContext())!

 UIGraphicsEndImageContext()

 UIImageWriteToSavedPhotosAlbum(newImage, nilnilnil)

 画顶部status bar

 UIGraphicsBeginImageContextWithOptions(CGSize(width:KDeviceWidth, height:20),false,0)

                   UIApplication.shared.keyWindow?.rootViewController?.view.layer.render(in:UIGraphicsGetCurrentContext()!)

                   let image:UIImage =UIGraphicsGetImageFromCurrentImageContext()!

                   let barImageView:UIImageView =UIImageView.init(frame:CGRect(x:0,y:0,width:KDeviceWidth,height:20))

                    barImageView.image = image

                   UIApplication.shared.keyWindow?.insertSubview(barImageView, aboveSubview: (UIApplication.shared.keyWindow?.subviews.last)!)



二、错误的方案(1)

let portionRect = CGRect(x:0, y:0, width:self.view.frame.width, height:self.cameraView.frame.height)           UIGraphicsBeginImageContextWithOptions(portionRect.size, false, UIScreen.main.scale

let ctx:CGContext? = UIGraphicsGetCurrentContext()          

let path:UIBezierPath = UIBezierPath.init(rect: portionRect)

path.addClip()

  self.cameraView.layer.render(in: ctx!)//这里换成self.view 并将框架换成portionRect2不行

let topCameraImage:UIImage? = UIGraphicsGetImageFromCurrentImageContext()

let size = CGSize(width:cameraView.frame.size.width,height:cameraView.frame.size.height)       

topCameraImage?.draw(in: CGRect(x:0 ,y:0 ,width:size.width ,height:size.height))

 用这种方式去获取合成的图片,缺点,最后绘成的图片的边框会少一个边框

三、错误方案(2)

//get complex image

 let portionRect = CGRect(x:0, y:0, width:(self.goodsBackView?.frame.width)!, height:(self.goodsBackView?.frame.height)!)

 UIGraphicsBeginImageContextWithOptions(portionRect.size, false, 0)

 let backImage:UIImage = UIImage.init(named: "dialog_full_holo_light")!.resizableImage(withCapInsets: UIEdgeInsets(top:8,left:8,bottom:8,right:8))

  backImage.draw(in: portionRect)

 goodsImageView?.image?.draw(in: CGRect(x:20 ,y:20,width:(self.goodsBackView?.frame.width)! - 40,height:(self.goodsBackView?.frame.height)! - 40))

//  goodsImageView?.image?.draw(in: CGRect(x:20 ,y:20,width:(self.goodsBackView?.frame.width)! - 40,height:(self.goodsBackView?.frame.height)! - 40), blendMode: .clear, alpha: 1)

图片一张一张绘制,结果,拉伸的图片绘制完成之后出现莫名的的细黑线,怎么都不好解决。

四、

        UIGraphicsBeginImageContextWithOptions(CGSize(width:KDeviceWidth,height:44), false, 0);

        self?.navigationController?.navigationBar.drawHierarchy(in: CGRect(x:0,y:0,width:KDeviceWidth,height:44), afterScreenUpdates: true)

        let navigationBarImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!

        UIGraphicsEndImageContext()

        let navigationBarImageView:UIImageView = UIImageView.init(frame: CGRect(x:0,y:20,width:KDeviceWidth,height:44))

        navigationBarImageView.image = navigationBarImage

        UIApplication.shared.keyWindow?.insertSubview(navigationBarImageView, aboveSubview: (UIApplication.shared.keyWindow?.subviews.last)!)

       

        UIGraphicsBeginImageContextWithOptions(CGSize(width:KDeviceWidth, height:20), false, 0)

        UIApplication.shared.keyWindow?.rootViewController?.view.layer.render(in: UIGraphicsGetCurrentContext()!)

        let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!

        let barImageView:UIImageView = UIImageView.init(frame: CGRect(x:0,y:0,width:KDeviceWidth,height:20))

        barImageView.image = image

        UIApplication.shared.keyWindow?.insertSubview(barImageView, aboveSubview: (UIApplication.shared.keyWindow?.subviews.last)!)

        UIGraphicsBeginImageContextWithOptions(CGSize(width:KDeviceWidth,height:44), false, 0);

        let tarbar:UITabBarController = UIApplication.shared.keyWindow?.rootViewController as! UITabBarController

        tarbar.selectedViewController?.navigationController?.navigationBar.drawHierarchy(in: CGRect(x:0,y:0,width:KDeviceWidth,height:44), afterScreenUpdates: true)

        let navigationBarImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!

        UIGraphicsEndImageContext()

        let navigationBarImageView:UIImageView = UIImageView.init(frame: CGRect(x:0,y:20,width:KDeviceWidth,height:44))

        navigationBarImageView.image = navigationBarImage

        UIApplication.shared.keyWindow?.insertSubview(navigationBarImageView, aboveSubview: (UIApplication.shared.keyWindow?.subviews.last)!)

        

        UIGraphicsBeginImageContextWithOptions(CGSize(width:KDeviceWidth, height:20), false, 0)

        UIApplication.shared.keyWindow?.rootViewController?.view.layer.render(in: UIGraphicsGetCurrentContext()!)

        let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!

        let barImageView:UIImageView = UIImageView.init(frame: CGRect(x:0,y:0,width:KDeviceWidth,height:20))

        barImageView.image = image

        UIApplication.shared.keyWindow?.insertSubview(barImageView, aboveSubview: (UIApplication.shared.keyWindow?.subviews.last)!)

    }

//在窗口绘图,截取窗口顶部的视图

          let screenWindow:UIWindow = UIApplication.shared.keyWindow!

          UIGraphicsBeginImageContext(CGSize(width:KDeviceWidth,height:88));

          screenWindow.layer.render(in: UIGraphicsGetCurrentContext()!)

          let viewImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!;

          UIGraphicsEndImageContext();

         let barImageView:UIImageView = UIImageView.init(frame: CGRect(x:0,y:0,width:KDeviceWidth,height:88))

          barImageView.image = viewImage

         barImageView.tag = 1000

         UIApplication.shared.keyWindow?.addSubview(barImageView)

五:总结

有图片拉伸的地方的绘制可能出现各种问题,对图片像素,对接口的精确理解,对绘制图片的精准认识,提升效率.  





原创粉丝点击