1. 程式人生 > >IOS多媒體-影象縮放及CoreImage及模糊效果

IOS多媒體-影象縮放及CoreImage及模糊效果

將圖片縮放到指定比例

func test1() {
        //將圖片縮放到指定比例
        let image = UIImage(named: "Pic4")
        let scaledImage = scaleImage(image: image!, newSize: CGSize(width: 180, height: 180))
        let imageView = UIImageView(image: scaledImage)
        imageView.center = CGPoint(x: 160, y: 160)
        self.view.addSubview(imageView)
    }
    //將圖片縮放到指定比例
    func scaleImage(image:UIImage , newSize:CGSize) -> UIImage {
        let imageSize = image.size
        let oldWidth = imageSize.width
        let oldHight = imageSize.height
        //計算出比例
        let widthFactor = newSize.width/oldWidth
        let hightFactor = newSize.height/oldHight
        //獲取最小比
        let scaleFactor = (widthFactor>hightFactor) ? hightFactor : widthFactor
       //計算出新的尺寸
        let width = scaleFactor * oldWidth
        let height = scaleFactor * oldHight
        //常見一個新的顯示區域
        let targetSize = CGSize(width: width, height: height)
        //建立繪圖上下文環境
        UIGraphicsBeginImageContext(targetSize)
        //將影象物件 畫入之前計算的新尺寸裡
        image.draw(in: CGRect(x: 0, y: 0, width: width, height: height))
        //獲取上下文裡的內容,將內容寫入新的影象物件
        let newImage = UIGraphicsGetImageFromCurrentImageContext();
        return newImage!
    }

使用圖形上下文轉換圖片為灰度圖 

func test2() {
        let image = UIImage(named: "Pic4")
        let scaledIMage = grayImage(image: image!)
        let imageView = UIImageView(image: scaledIMage)
        imageView.center = CGPoint(x: 160, y: 160)
        self.view.addSubview(imageView)
    }
    //使用圖形上下文轉換圖片為灰度圖
    func grayImage(image:UIImage) -> UIImage {
        let imageSize = image.size
        let width = imageSize.width
        let height = imageSize.height
        //建立灰度色彩空間物件
        let spaceRef = CGColorSpaceCreateDeviceGray()
        let context = CGContext(data: nil, width:Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: spaceRef, bitmapInfo:CGBitmapInfo().rawValue)!
        
        let rect = CGRect(x: 0, y: 0, width: imageSize.width, height: imageSize.height  )
        context.draw(image.cgImage!, in: rect)
        let grayImage = UIImage(cgImage: context.makeImage()!)
        return grayImage
    }

利用圖片處理框架,將圖片轉成單色樣式

func test3() {
        let image = UIImage(named: "Pic4")
        let imageView = UIImageView(image: image)
        self.view.addSubview(imageView)
        
        //初始化一個影象物件 載入之前匯入圖片
        let ciImage = CIImage(image: image!)
        
        let color = CIColor(red: 0.8, green: 0.6, blue: 0.4)
        //初始化濾鏡物件 設定濾鏡物件為單色調濾鏡
        let filter = CIFilter(name: "CIColorMonochrome")
        //設定單色調濾鏡的輸入顏色值
        filter?.setValue(color, forKey: kCIInputColorKey)
        //設定單色調濾鏡的顏色濃度值
        filter?.setValue(1.0, forKey: kCIInputIntensityKey)
        //設定需要應用單色調濾鏡的影象
        filter?.setValue(ciImage, forKey: kCIInputImageKey)
        //獲得應用單色調濾鏡後的影象
        let outimage = filter?.outputImage
        imageView.image = UIImage(ciImage: outimage!)
    }

使用圖片處理框架調整影象的色相

func test4()  {
        let image = UIImage(named: "Pic4")
        let imageVIew = UIImageView(image: image)
        self.view.addSubview(imageVIew)
        
        let ciImage = CIImage(image: image!)
        //初始化濾鏡物件。調整為色相調整濾鏡
        let filter = CIFilter(name: "CIHueAdjust")
        //設定色相調整濾鏡的輸入角度值為30度
        filter?.setValue(3.14/6, forKey: kCIInputAngleKey)
        //設定需要應用色相調整濾鏡的影象
        filter?.setValue(ciImage, forKey: kCIInputImageKey)
        let outImage = filter?.outputImage
        imageVIew.image=UIImage(ciImage: outImage!)
    }

使用圖片框架新增馬賽克

func test5() {
        let image = UIImage(named: "Pic4")
        let imageVIew = UIImageView(image: image)
        self.view.addSubview(imageVIew)
        
        let ciimage = CIImage(image: image!)
        //初始化一個濾鏡物件,設定為畫素濾鏡
        let filter = CIFilter(name: "CIPixellate")
        filter?.setDefaults()//採用預設配置選項
        //設定需要該濾鏡的影象
        filter?.setValue(ciimage, forKey: kCIInputImageKey)
        let outImage = filter?.outputImage
        imageVIew.image = UIImage(ciImage: outImage!)
    }

 使用影象框架新增模糊效果

 func test6() {
        let image = UIImage(named: "Pic4")
        let imageVIew = UIImageView(image: image)
        self.view.addSubview(imageVIew)
        //從8.0開始系統提供了模糊效果。判斷如果是8.0則執行模糊的效果
        if #available(iOS 8.0, *) {
            //初始化一個效果模糊物件。可快速製作毛玻璃特效
            let blur = UIBlurEffect(style: .light)
            // 初始化一個基於模糊效果的視覺效果檢視
            let blurView = UIVisualEffectView(effect: blur)
            //設定模糊檢視的位置
            blurView.frame = CGRect(x: 40, y: 40, width: 200, height: 200)
            //設定模糊檢視的圓角為30
            blurView.layer.cornerRadius=30
            //設定模糊檢視層的遮罩覆蓋屬性 進行邊界裁切
            blurView.layer.masksToBounds = true
            imageVIew.addSubview(blurView)
            
        }else{
            let dialog = UIAlertController(title: "提示", message: "8.0以下不支援模糊功能", preferredStyle: UIAlertControllerStyle.alert)
            let ok = UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: nil)
            dialog.addAction(ok)
            self.present(dialog,animated: true,completion: nil)
        }
    }