Swift Image 的处理

732 阅读1分钟
extension UIImage {
   /* 限定图片的大小 */
    func resize(width:CGFloat, height:CGFloat) -> UIImage {
        let myImageSize = CGSizeMake(width, height)
        UIGraphicsBeginImageContextWithOptions(myImageSize, false, 0.0)
        let myImageRect = CGRectMake(0, 0, myImageSize.width, myImageSize.height)
        self.drawInRect(myImageRect)
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
      }

    /*生成纯色图片*/
    class func imageColored(color: UIColor) -> UIImage! {
        let rect = CGRect(x: 0, y: 0, width: 0.5, height: 0.5)
        UIGraphicsBeginImageContextWithOptions(rect.size, CGColorGetAlpha(color.CGColor) == 1, 0)
        let context = UIGraphicsGetCurrentContext()!
        CGContextSetFillColorWithColor(context, color.CGColor)
        CGContextFillRect(context, rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
      }


    /*  压缩图片,最大为1M  */
    func compressImage(maxImageSize: CGFloat, maxSizeWithKB: CGFloat) -> NSData {
        var maxImgWithKB = maxSizeWithKB
        var maxImgSize = maxImageSize
        
        if maxImgWithKB <= 0 {
            maxImgWithKB = 1024
        }
        
        if maxImgSize <= 0 {
            maxImgSize = 1024
        }
        
        // 调整分辨率
        var newSize = CGSizeMake(self.size.width, self.size.height)
        let tempHeight = newSize.height / maxImgSize
        let tempWidth = newSize.width / maxImgSize
        
        if (tempWidth > 1.0 && tempWidth > tempHeight) {
            newSize = CGSizeMake(self.size.width / tempWidth, self.size.height / tempHeight)
        } else if (tempHeight > 1.0 && tempWidth < tempHeight) {
            newSize = CGSizeMake(self.size.width / tempHeight, self.size.width / tempHeight)
        }
        
        UIGraphicsBeginImageContext(newSize)
        self.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        // 调整大小
        var imageData = UIImageJPEGRepresentation(newImage!, 1.0)
        var sizeOriginKB = CGFloat(imageData!.length) / 1024
        
        var resizeRate: CGFloat = 0.9
        while (sizeOriginKB > maxImgWithKB && resizeRate > 1.0) {
            imageData = UIImageJPEGRepresentation(newImage!, resizeRate)
            sizeOriginKB = CGFloat(imageData!.length) / 1024
            resizeRate -= 0.1
        }
        
        return imageData!
      }


    /*图片着色*/
    func tint(color: UIColor, blendMode: CGBlendMode) -> UIImage {
        let drawRect = CGRectMake(0.0, 0.0, size.width, size.height)
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        color.setFill()
        UIRectFill(drawRect)
        drawInRect(drawRect, blendMode: blendMode, alpha: 1.0)
        let tintedImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return tintedImage
      }
    
    func tintTheme() -> UIImage {
        return self.tint(UIStyles.getThemeColor(), blendMode: CGBlendMode.DestinationIn)
      }
    
    func tintGray() -> UIImage {
        return self.tint(UIColor.grayColor(), blendMode: CGBlendMode.DestinationIn)
      }
    
    func tintLightGray() -> UIImage {
        return self.tint(UIColor.lightGrayColor(), blendMode: CGBlendMode.DestinationIn)
      }
}